Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-02-2009, 05:30 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 17
Unhappy 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.

Any ideas?

Thanks
slobbymon is offline   Reply With Quote
Old 08-02-2009, 05:39 PM   #2 (permalink)
Former NeXTStep Developer
 
Join Date: Mar 2009
Posts: 997
Default

Post code.

joe
FlyingDiver is offline   Reply With Quote
Old 08-02-2009, 05:46 PM   #3 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 188
Default

Quote:
Originally Posted by slobbymon View Post
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.

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.
jsonli is offline   Reply With Quote
Old 08-02-2009, 06:52 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 17
Default

Quote:
Originally Posted by FlyingDiver View Post
Post code.

joe
Here is some code.

I create the array in the function like this:

HTML Code:
NSMutableArray * recs =[[NSMutableArray alloc] initWithCapacity:4];
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];
	
}
slobbymon is offline   Reply With Quote
Old 08-02-2009, 06:56 PM   #5 (permalink)
Registered Member
 
Join Date: Nov 2008
Posts: 188
Default

Quote:
Originally Posted by jsonli View Post
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.
+1
jsonli is offline   Reply With Quote
Old 08-02-2009, 07:00 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 17
Default

Quote:
Originally Posted by jsonli View Post
+1
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?
slobbymon is offline   Reply With Quote
Old 08-02-2009, 07:13 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,129
Default

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.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 08-02-2009, 10:38 PM   #8 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 17
Default

Quote:
Originally Posted by BrianSlick View Post
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.

Thanks again so much!
slobbymon is offline   Reply With Quote
Reply

Bookmarks

Tags
autorelease, leaks, memory, nsmutablearray, release

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 238
15 members and 223 guests
ADY, AragornSG, Dani77, Dattee, Duncan C, fkmtc, HDshot, HemiMG, Promo Dispenser, Punkjumper, Rudy, sacha1996, sneaky, spiderguy84, theone8one
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,885
Threads: 89,231
Posts: 380,768
Top Poster: BrianSlick (7,129)
Welcome to our newest member, bookesp
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:16 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0