tags:

views:

81

answers:

3

I am using this line in my code

NSMutableArray * NewsFeedArray; NewsFeedArray =[[[NSMutableArray alloc]init]retain];

Where could i release object of NSMutableArray ? and Why we want to release that object... In my project i release the object in dealloc method ..But its take some more time the release the object....

Can anyone help me? What exactly doing this?

Thanks in advance...

A: 

Release the objects you add to the NewsFeedArray straight after you add them. The fact that you add them to an array makes their retain count go up by one. The array 'owns' them, and when you release the array, all the objects that are in it will have their retain count decreased by one (generally releasing them too).

nevan
+1  A: 
NewsFeedArray =[[[NSMutableArray alloc] init] retain];

This line of code actually bumps your object's retain count up to 2. That is probably why you're experiencing the object not being completely released when you expect it to.

Jacob H. Hansen
A: 

You need something like this when you start (possibly in your init method):

NSMutableArray* news = [[NSMutableArray alloc] initWithCapacity:10];

Note that you don't need to retain it.

To add stuff:

RandomObject* obj = [[RandomObject alloc] init];
// set properties
[news addObject:obj];
[obj release];

You can release the new object as adding it to the array increased its reference count.

And finally, you put the following in your dealloc method:

[news release];

Releasing the array will automagically release every object that it holds.

Stephen Darlington