When and why to release an object

When and why to release an object

I am using these lines in my code

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

Where should I release this NSMutableArray, and why do I want to release that object? In my project, I release the object in my dealloc method, but it takes more time to do that.

Can anyone explain this?

How to show a local image in the webview?


Mouseover in NSTableView
NewsFeedArray =[[[NSMutableArray alloc] init] retain]; 
This line of code actually bumps your object's retain count up to 2. Google App Engine with ClientLogin Interface for Objective-CThat is probably why you're experiencing the object not being completely released when you expect it to.. Lower case “k” in Cocoa
Sample Cocoa app, ref counting, [pool drain] yields EXC_BAD_ACCESS?HashTables in Cocoa


Writing to an iframe from NSTextView
Release the objects you add to the NewsFeedArray straight after you add them. How to use NSString drawInRect to center text?The fact this 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 this are in it will have their retain count decreased by one (generally releasing them too)..


You need any thing like this when you start (possibly in your init method):.
NSMutableArray* news = [[NSMutableArray alloc] initWithCapacity:10]; 
Note this you don't need to retain it.. To add stuff:.
RandomObject* obj = [[RandomObject alloc] init]; // set properties [news addObject:obj]; [obj release]; 
You must 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 this it holds..

83 out of 100 based on 38 user ratings 193 reviews