Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retain, alloc, properties ... Topic to make your Obj-c life easier !

The more I code, the more I get lost ... so I decided to create a topic entirely dedicated to the memory management for me (and others) not to waste hours understanding obj-c basics ... I'll update it as new questions are asked !

Okay below is some examples :

// myArray is property (retain)
myArray = otherArray;

//myArray isn't a property
myArray = otherArray;

//myArray is a property (retain)
myArray = [[NSArray alloc] init];

//myArray isn't a property
myArray = [[NSArray alloc] init];

--- So, if I understand ... when you put self.myArray you tell Xcode to use the getter or setter but when you just do myArray, you are responsible for everything, right ?

[SOLVED] UPDATE1 : Is there a difference between :

//myArray is a property
myArray = otherArray; // it is only a reference, releasing otherArray will imply releasing myArray
self.myArray = otherArray; // otherArray is sent a retain message so releasing otherArray will still keep myArray in memory

--- Yes, there is a difference (see comments above)

[SOLVED] UPDATE2 : Is myArray below equal to nil ?

NSArray *myArray;

--- Kubi : Yes, it is equal to nil.

[SOLVED] UPDATE3 : does it count for 2 retains ? One retain from self and one retain from alloc ? Is this a memory leak ?

self.myArray = [[NSArray alloc] init];

--- Kubi : Yes, this is a memory leak !

[SOLVED] UPDATE4 : the property takes care of everything ? no need to alloc or release ?

self.myArray = [NSArray array];

--- We here use the setter so that the array is retained properly

[SOLVED] UPDATE5 : Are these two blocks the same ?

//myArray is a retained property

self.myArray = [NSArray array]; //retain
self.myArray = nil; //release and set to nil

myArray = [[NSArray alloc] initWithArray]; //retain
self.myArray = nil; //release and set to nil

--- Kubi : Yes, they're identical

Thanks for the time.

Gotye.

like image 908
gotye Avatar asked Mar 22 '10 23:03

gotye


1 Answers

First, I'm assuming that you have a property called myArray and an iVar called myArray? If so, cases 1,2 are identical and 3,4 are identical. If you need to set a property of your current class, you must do it through one of the following methods:

self.myArray = otherArray;
[self setMyArray:otherArray];

the line myArray = otherArray will only set the iVar, never the property.

Second part, you're asking about memory management. Step one: read Apple's Guide. It really is required reading. Don't worry if you don't completely understanding, keep reading it once a month and it'll crystalize eventually.

Step two: remember this rule of thumb: If you alloc, copy, new, or retain an object, you are responsible for releasing that object, if you don't, it will be leaked.

In all other cases, you are not responsible for releasing the object, but it will be released eventually. If you need to keep it around, you need to retain it (and, of course, release it later).

Back to your example, in the first two cases, if you don't retain myArray it'll be released at some point after this code block. If you try and message that object later, you'll get an error. In the second two cases, if you don't release the myArray object at some point, it will be leaked.


Update 1 Very big difference. The two lines are completely different. The important thing to realize about dot syntax is that these two lines are exactly equivalent:

self.myArray = otherArray;
[self setMyArray:otherArray];

Notice the second line is a method call. Theoretically you could put anything you wanted in that method. You could set myArray to nil, or set it to someOtherArray, or update twitter or whatever.


Update 2 Yup, pointers in Obj-C are initialized to nil.


Update 3 Exactly. If the myArray property is declared as retain and you're using the default synthesizers you will be causing a memory leak.

Update 5 Also exactly right.

like image 129
kubi Avatar answered Oct 03 '22 15:10

kubi