Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Should I release object in dealloc function?

Tags:

ios

release

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!

like image 422
jxdwinter Avatar asked Jul 03 '26 12:07

jxdwinter


2 Answers

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.

like image 169
glenstorey Avatar answered Jul 06 '26 05:07

glenstorey


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

like image 24
sElanthiraiyan Avatar answered Jul 06 '26 06:07

sElanthiraiyan