Memory leak may be related to NSMutableArray or NSArray
Hi Everyone,
I have a memory leak issue I can't quite figure out. I have a function that returns an NSMutableArray of custom objects. That array is then used as a member variable for a View. When I release the view I release every object in the array and then the array itself, but it is still leaking. That is, the objects in the array are released, but the memory allocated for the array is NOT released.
If I return the autorelease version of the array, my application crashes the second time I assign the member variable to the array returned by the data (when the view is loaded a second time).
It seems like the ref count changing by 1 should either fix the leak or have the leak remain, but not crash the application.
I'll try to distill the code down, but this seems to happen an all cases where i return an array from a function and assign it to a member variable in one of my views. I have created custom setter methods where I release the items in the array and then the array before assigning the new array to the member variable, but this is the most recent result I get.
I have a memory leak issue I can't quite figure out. I have a function that returns an NSMutableArray of custom objects. That array is then used as a member variable for a View. When I release the view I release every object in the array and then the array itself, but it is still leaking. That is, the objects in the array are released, but the memory allocated for the array is NOT released.
If I return the autorelease version of the array, my application crashes the second time I assign the member variable to the array returned by the data (when the view is loaded a second time).
It seems like the ref count changing by 1 should either fix the leak or have the leak remain, but not crash the application.
I'll try to distill the code down, but this seems to happen an all cases where i return an array from a function and assign it to a member variable in one of my views. I have created custom setter methods where I release the items in the array and then the array before assigning the new array to the member variable, but this is the most recent result I get.
Any ideas?
Thanks
When the array itself is deallocated, it will send a release to each of its items - you don't need to do it yourself.
But to the point, you haven't balanced retains/alloc and releases/autoreleases.
Add custom objects like the following, where all custom:
HTML Code:
[recs addObject:customObj];
[customObj release];
Then I return the array like:
HTML Code:
return recs;
The caller puts the result into a member function.
HTML Code:
self.recs = [XXX getRecs];
The setter for this is
HTML Code:
-(void) setRecs:(NSMutableArray *)input {
if (recs !=nil) {
for (id obj in recs) {
[obj release];
}
//[recs release] if i uncomment this it crashes
}
recs = [input retain];
}
Thanks for the reply, but if I uncomment the [recs release] in the setter method, or return [recs autorelease] the program crashes the second time i set the value.
Is that what you meant, or am I missing some other area?
Are you still releasing each object in the array manually? Don't do that.
If the entire purpose of your custom setter was to manually release each object in the array, just get rid of the setter and use the one you get with a synthesized property.
Are you still releasing each object in the array manually? Don't do that.
If the entire purpose of your custom setter was to manually release each object in the array, just get rid of the setter and use the one you get with a synthesized property.
Thanks for your help everyone! By doing this I fixed the leaks. I didn't realize that releasing the array released all objects in there. I think my setter functions were causing some additional ref counting or something. When I removed the setter and just put the release call in dealloc everything gets cleaned up. Not sure why my setter methods were screwing things up, but I'll have to watch out for that in the future.