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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 06-04-2011, 01:40 PM   #1 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default Object Alloc Memory Leak?

I have several objects that have NSMutableArrays which will contain other objects that contain ... etc.

When I delete the highest object in the hierarchy, I have memory leaks in all of the [NSObject init] method. They all are identical and look like the following:

Code:
- (NSObject1 *)init
{
	arrayOfObject2 = [[NSMutableArray alloc] init];
	return self;
}
And their dealloc methods look like:
Code:
- (void)dealloc
{
	[super dealloc];
	for (int i = 0; i < [arrayOfObject2 count]; i++)
	{
		[[arrayOfObject2 objectAtIndex:i] release];
	}
	arrayOfObject2 = nil;
	[arrayOfObject2 release];
}
I don't think that the lower class's dealloc methods are being called because I put an NSLog message in one, and it did not appear.

Any thoughts on why this memory leak is happening? Again, it only occurs when I delete the higher objects.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 01:57 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

1. You're doing something really wrong if you are releasing the items in an array while they are still in the array.

2:

Code:
arrayOfObject2 = nil;
...ok, so arrayOfObject2 is now nil, so what exactly does this do after that:

Code:
[arrayOfObject2 release];
3. That init method makes me cry.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:08 PM   #3 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
1. You're doing something really wrong if you are releasing the items in an array while they are still in the array.

2:

Code:
arrayOfObject2 = nil;
...ok, so arrayOfObject2 is now nil, so what exactly does this do after that:

Code:
[arrayOfObject2 release];
3. That init method makes me cry.
Instead of simply pointing out my errors, could you explain what I'm doing wrong? I'm sorry for being relatively new at this. I haven't seen much code for creating your own object, so I tried to do it on my own.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:11 PM   #4 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

No, first you explain why you are doing what you are doing. I know you haven't seen any examples of an init method like that, and I'm reasonably certain that you have seen init methods in other cases (hint: view controllers) that should give you an idea of what needs to happen.

Being new is no excuse. You knew enough to write the code that you have, and you know enough to use instruments. That's a cop out. If you can't explain why you did something, THAT is the first problem that needs to be addressed.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:14 PM   #5 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

I made that init the way I did because when I tried to add objects to that array in the code, it did not work because the array had not be initialized, so I initialized it. That makes sense to me. I'm not sure what else I'm supposed to do there to prevent you from crying.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:16 PM   #6 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Then you're not even pretending to look at other init method examples to see what you should do. If you aren't going to do any work, why should I?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:26 PM   #7 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
- (id)init
{
self = [super init];
if (self != nil)
{
arrayOfObject2 = [[NSMutableArray alloc] init];
}
return self;
}
Is this what you're talking about? And do you think this is what's causing the memory leak?
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:27 PM   #8 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

There you go.

Try it. Do you still leak?

Oh, and I should also add:

4. [super dealloc] should be the last thing you do in dealloc, not the first.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:30 PM   #9 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
There you go.

Try it. Do you still leak?

Oh, and I should also add:

4. [super dealloc] should be the last thing you do in dealloc, not the first.
Yes. Same leaks, unfortunately.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:32 PM   #10 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Ok, so we've addressed #3. That still leaves #1 and #2.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:33 PM   #11 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
- (void)dealloc
{
for (int i = 0; i < [arrayOfObject2 count]; i++)
{
[arrayOfObject2 removeObjectAtIndex:i];
}
[arrayOfObject2 release];
[super dealloc];

}
Is that the proper way to do the dealloc?
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:35 PM   #12 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

It's better than your first attempt. You do not need to manually empty out the array.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:42 PM   #13 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
It's better than your first attempt. You do not need to manually empty out the array.
I'm not sure what you mean exactly by that. But I've found this method. Is that what you're talking about?

Quote:
- (void)dealloc
{
[arrayOfObject2 removeAllObjects];
[arrayOfObject2 release];
[super dealloc];

}
I'm still getting leaks.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:45 PM   #14 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

No, what I'm saying is that if the array is going to die at this point, it will automatically take care of removing the items for you. I guess it doesn't hurt anything to clear it out yourself, but it's not necessary.

But otherwise, your init method is now fine, and your dealloc method is now fine. So if you are still leaking, you have issues elsewhere.

Is your dealloc method being called now?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:46 PM   #15 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

You don't need removeAllObjects at all. What exactly is reporting the leak and with what message? Instruments, or is just Xcode giving you a warning? Are you retaining the array anywhere else?
baja_yu is offline   Reply With Quote
Old 06-04-2011, 02:56 PM   #16 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Quote:
Originally Posted by BrianSlick View Post
No, what I'm saying is that if the array is going to die at this point, it will automatically take care of removing the items for you. I guess it doesn't hurt anything to clear it out yourself, but it's not necessary.

But otherwise, your init method is now fine, and your dealloc method is now fine. So if you are still leaking, you have issues elsewhere.

Is your dealloc method being called now?
Yes. Now, how do I safely remove all of these objects? What I was doing was starting at the top and working my way down to the lowest object. I deleted all of the objects at the lower level and worked my way back up.

Is this the correct way to do it?
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:58 PM   #17 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

I don't understand the question. You say that the dealloc method is being called, so what do you mean by "safely remove all of these objects"?
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

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

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 06-04-2011, 02:58 PM   #18 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

To Baja, the leaks were coming from Leaks and they were from the class's init methods.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:59 PM   #19 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Oh, sorry. I meant that it wasn't being called haha. I apologize.
ledoslover is offline   Reply With Quote
Old 06-04-2011, 02:59 PM   #20 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

When you put an object in an NSArray, the array will retain it, so you should release it (if you allocated it prior) after adding it to the array. When you release an array it will send a release message to all of its objects as well so that will automatically remove them.
baja_yu is offline   Reply With Quote
Old 06-04-2011, 03:01 PM   #21 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

So if I have a 3 dimensional array, all I have to do is release the first level of the array?
ledoslover is offline   Reply With Quote
Old 06-04-2011, 03:06 PM   #22 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

There are no multidimensional NSArrays. The closest thing would an an array of arrays. So following logic, the top array would send release to the arrays it holds and they would in turn send release to objects in them. This should be easy to deduce.
baja_yu is offline   Reply With Quote
Old 06-04-2011, 03:11 PM   #23 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

I have 3 levels of arrays. An object with an array of an object that has an array of an object, etc.

In order to delete the highest object, I just need to say [highestObject release]?
ledoslover is offline   Reply With Quote
Old 06-04-2011, 05:47 PM   #24 (permalink)
Registered Member
 
Join Date: May 2011
Posts: 62
ledoslover is on a distinguished road
Default

Code:
- (id)init // init for Object1
{
	self = [super init];
	if (self != nil)
	{
		arrayofObject2 = [[NSMutableArray alloc] init];
	}
	return self;
}
If anybody is still reading this thread, the leak is coming from the array alloc line, and I have no idea why still!
ledoslover is offline   Reply With Quote
Old 06-04-2011, 06:10 PM   #25 (permalink)
Nuisance Developer
 
Join Date: Jul 2009
Location: Italy
Posts: 4,691
dany_dev is on a distinguished road
Default

read this...http://www.iphonedevsdk.com/forum/ip...roperties.html
__________________
dany_dev is offline   Reply With Quote
Reply

Bookmarks

Tags
memory leak, nsmutablearray

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: 326
5 members and 321 guests
Dnnake, iOS.Lover, jenniead38, pbart, Wikiboo
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,663
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, LezB44
Powered by vBadvanced CMPS v3.1.0

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