Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to clear an NSMutableArray of custom objects without creating memory leaks?

If I have an NSMutableArray of custom objects, how can I must easily clear the array without causing any memory issues? Assume that the custom object class has a dealloc method in it which correctly released an instance variables etc.

For example is it ok to use the NSArray "removeAllObjects" method?

  • If yes - how does this work - does "removeAllObjects" call the "dealloc" method on each object as it removes them

  • If no - what would be the easiest approach to use?

EDIT (after 4 replies) - One last question of clarification after the great replies - I'm still not quite sure about the instance variables/properties in my custom object that I have set to retain? These seem to be only released via the the "dealloc" method in my custom object class, where we do this manually along with [super release].

So if, re clearing an array, if I do a removeAllObjects, and then NSArray issues a "release" to my custom objects, but doesn't call "dealloc", then how do my instance variables get released?

like image 569
Greg Avatar asked Sep 29 '11 11:09

Greg


2 Answers

removeAllObjects will remove the object from the array. This process will send a release message to the object and this will decrease its reference count. When the reference count reaches zero the object will be deallocated.

don't do it like this, because it will leak.

NSObject *object = [[NSObject alloc] init];       + 1
[array addObject:object];                         + 1
[array removeAllObjects];                         - 1
                                                =======
                                                = + 1 -> Leak

this is the correct way:

NSObject *object = [[[NSObject alloc] init] autorelease]; + 1 (from alloc) - 1 (from autorelease)
[array addObject:object];                         + 1
[array removeAllObjects];                         - 1
                                                =======
                                                =   0 -> Object will be deallocated

Instead of calling removeAllObjects you could just release the array. If an array is deallocated everything that's inside of it gets released and if there is no other reference to the object it will be deallocated.

like image 170
Matthias Bauch Avatar answered Oct 22 '22 23:10

Matthias Bauch


Yep, just call removeAllObjects. Just to be sure, you don't call retain when you add an object to an array or when you create an array with objects. That's done for you automatically.

Regarding dealloc, again that will be done automatically, and you can't predict when.

The only thing you need to have in the dealloc is the array object itself. That is, assuming it's an instance variable or ivar?

To check everything is good, run the Analyzer using Product -> Analyze. And then give the app a profile in Instruments using the Leaks instrument to check that none of your code is causing any memory leaks.

like image 25
Max MacLeod Avatar answered Oct 22 '22 22:10

Max MacLeod