Memory

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

  1. macrumors newbie

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


    Why? I release the object - sceneWithDistance.

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

    Joined:
    Mar 13, 2008
    #2
    Code:
    [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. thread starter macrumors newbie

    Joined:
    Jan 7, 2009
    #3
    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.

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

    Joined:
    Aug 10, 2008
    #4
    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. Guest

    caveman_uk

    Joined:
    Feb 17, 2003
    Location:
    Hitchin, Herts, UK
  6. macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #6
    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