Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

illwi11

macrumors newbie
Original poster
Jan 7, 2009
3
0
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];
}
 

drivefast

macrumors regular
Mar 13, 2008
128
0
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.
 

illwi11

macrumors newbie
Original poster
Jan 7, 2009
3
0
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?
 

beachdog

macrumors member
Aug 10, 2008
86
0
It says the leaked object is a NCSFString - the only object that is a string is path.

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?
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.