Reputation: 2839
I'm curious what happens with the memory of the objects that I push with [array addObject:object]
when I use [array release]
Upvotes: 0
Views: 69
Reputation: 48398
When you call
[array addObject:object]
array
retains object
, thereby incrementing its retain count.
Later, when array
is sent the message dealloc
, it calls release
on object
.
To avoid memory leaks, you may have to release object
after you add it to the array
, e.g.
NSObject *object = [[NSObject] alloc] init];
[array addObject:object];
[object release];
Make sure you review the Memory Management Programming Guide to be certain that you are not over or under releasing object
.
Upvotes: 1
Reputation: 3634
When you do [array addObject: object], the array is retaining the object that is inserted into the array. To avoid memory leaks, don't forget to release the original object that was inserted, or else the object being inserted into the array will have a retain count of 2 instead of 1:
SomeClassObject *obj = [[SomeClassObject alloc] init];
[array addObject:obj];
[obj release];
Since the array owns the objects that are inside of the array (holds a pointer to that object and did a retain as explained previously), when you release the array, the array knows to release any objects that are inside of it. The work is done for you!
Upvotes: 1
Reputation: 10312
addObject does a retain on that object you add. And when you release the array it automatically calls release on all objects it holds.
Upvotes: 1
Reputation: 19789
NSArrays retain added objects, so you don't have to add extra retains yourself, and can just release the array when done.
Upvotes: 0