Discussion in 'iOS Programming' started by illwi11, Jan 7, 2009.

  1. illwi11 macrumors newbie

    Jan 7, 2009
    Instruments is saying that there is a leak at the line: NSMutableDictionary *sceneWithDistance = [[NSMutableDictionary alloc] initWithCapacity:numKeys];

    Why? I release the object - sceneWithDistance.

    Why? We release sceneWithDistance.
    - (NSMutableArray *) sortByDistance: (NSMutableArray *) toSort {
            NSMutableDictionary *sceneWithDistance = [[NSMutableDictionary alloc] initWithCapacity:numKeys];
            [sorted addObject:sceneWithDistance];
            [sceneWithDistance release];
  2. drivefast macrumors regular

    Mar 13, 2008
    [sorted addObject:sceneWithDistance];
    this holds a reference to your sceneWithDistance object. memory allocation in objective-c works differently from how perhaps you expect it to. in the faq of this forum there is a link to a decent article that explains the memory management in objective-c, it's a lecture i would recommend.
  3. illwi11 thread starter macrumors newbie

    Jan 7, 2009
    One more memory leak

    Thanks for the reply. I do understand the reference counting memory management. I do release the variable "sorted" later so the reference count of sceneWithDistance should also be decremented by 1. I'll look into it some more. Thank you for the response.

    I have another memory leak that is very confusing.

    Instruments is saying that there is a leak at this line: userCredentials = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    It says the leaked object is a NCSFString - the only object that is a string is path. However when I release this object ([path release]) , the application completely crashes on startup.

    - (NSDictionary *) retrieveUserCredentials{
        NSString *path=[NSTemporaryDirectory() stringByAppendingString:@"user.txt"];
        NSDictionary *userCredentials;
        userCredentials = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    Any ideas?
  4. beachdog macrumors member

    Aug 10, 2008
    To me it seems like you are handling 'path' correctly -- you are getting back an auto-released string and thus you should *not* explicitly release it.

    What about the string returned from NSTemporaryDirectory() ? Could that be the leak?
  5. caveman_uk Guest


    Feb 17, 2003
    Hitchin, Herts, UK
  6. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    path and NSTemporaryDirectory() are not the source of the leak. What is the contents of the userCredentials dictionary? Probably the leak is a string held in that dictionary.

Share This Page