Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The correct way to release variables in Objective-c

I know that in Objective-c there's a very easy way to declare variables like this:

NSArray* myArray;

@property(retain) NSArray* myArray;

@synthesize myArray;

This way you can use self.myArray as both the setter and getter while retaining the variable. However this will also allow you to do one more thing, which is avoiding yourself using dealloc. As far as I understand, this two lines are the same:

self.myArray = nil;
[myArray release];

My question is, which one is the preferred way? Is there any cases where one of them will work and the other one won't?

EDIT: Sorry, I meant release, not dealloc...

like image 398
Enrico Susatyo Avatar asked Jan 20 '23 20:01

Enrico Susatyo


1 Answers

You should never call dealloc yourself (except under very unorthodox circumstances).

Instead of dealloc, you should call [myArray release] and let the released process take care of this for you.

Have a look here for way more info than you'd probably like on a dealloc method as well.

like image 185
slycrel Avatar answered Jan 23 '23 10:01

slycrel