PDA

View Full Version : Memory




illwi11
Jan 7, 2009, 11:01 PM
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];
}



drivefast
Jan 7, 2009, 11:44 PM
[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
Jan 8, 2009, 12:17 AM
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?

beachdog
Jan 8, 2009, 08:16 AM
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?

caveman_uk
Jan 8, 2009, 09:11 AM
Don't assume all Apple code is leak free.

PhoneyDeveloper
Jan 8, 2009, 10:07 AM
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.