jxdwinter
jxdwinter

Reputation: 2369

iOS: Should I release object in dealloc function?

Im very confused with the memory management.

Declared variable allNoticeArray in .h file:

@interface NoticeViewController : UITableViewController
{   
    NSMutableArray *allNoticeArray;    
}
@property (nonatomic, retain) NSMutableArray *allNoticeArray;
@end

Alloc and init the variable in .m file:

@implementation NoticeViewController
@synthesize allNoticeArray;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.allNoticeArray = [[[NSMutableArray alloc] init] autorelease];
}
- (void)dealloc
{
    [super dealloc];
    /*
        ***should I release allNoticeArray here or not?***
    */
    //[allNoticeArray release];
}

Should I release the allNoticeArray in dealloc function or not?

Thank you in advance!

Upvotes: 2

Views: 2937

Answers (5)

SuperGuyAbe
SuperGuyAbe

Reputation: 535

When not using ARC you should use the release in the dealloc. You are right in saying that your retain in the .h file increases the retain count by one. The Alloc/Init creates an object with a retain count of one. The auto release would counter that retain, however your dealloc release would count the retain in the .h.

Setting self.allNoticeArray=nil; is not the same as a release, but the link from sElan is a great link.

Upvotes: 2

sElanthiraiyan
sElanthiraiyan

Reputation: 6268

Yes, you have to release the object. You could do the below in your dealloc method which will release your object.

self.allNoticeArray = nil;

REASON: Although you have autoreleased the array you have declared your property as retain. So the object will be retained and used. So to totally remove the object from memory you should again call release over it. You can learn everything about memory management here https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

Upvotes: 3

glenstorey
glenstorey

Reputation: 5154

It looks like you're manually managing your memory rather than using ARC.

If you're using IOS5 it might be easier for you to convert your project to ARC, then you won't have to worry about dealloc in this context.

If you don't want to use ARC you do need to release it in this context because you alloc'd it in viewDidLoad. You might also be interested in this article about dealloc.

Upvotes: 3

borrrden
borrrden

Reputation: 33423

The rule of pre-ARC memory management is this: if you alloc, retain, or copy an object you must release it later or it will leak. Your property is set to retain, so whatever value it holds will be retained. You must balance it with a release in dealloc.

Upvotes: 2

Dmorneault
Dmorneault

Reputation: 199

The short answer is NO, but you are doing a few things that are not recommended. In your code you are retaining your array twice in the single line. In the code below it is only retain once.

You should initialize the array like so -

NSMutableArray *array = [[NSMutableArray alloc] init] autorelease];
self.allNoticeArray = array;

I would recommend you look into using ARC, it makes memory management a non issue in IOS 5.

Upvotes: 1

Related Questions