IPhone memory management

There are several iPhone memory management concepts that have confused me, so I was hoping someone would be able to clarify them.

(a) Say that I call a function that returns an image, and I want to store this returned image in a variable so that I can assign it to other variables without having to call the image generation function again every time. At the moment, I am doing the following:

UIImage *storedImage = [someFunction returnImage];

and because storedImagenot alloced I am not releasing a variable.

However, should I be explicitly alloc'and free UIImageinstead?

UIImage *storedImage = [[UIImage alloc] init];
storedImage = [someFunction returnImage];
...do stuff...
[storedImage release];

What is meant when performing a direct assignment without alloc, rather than alloc'with a variable, and then assignment?

(b) init . :

self.arrayVariable = [[NSMutableArray alloc] init];

, , :

theArrayVariable = [[NSMutableArray alloc] init];
self.arrayVariable = theArrayVariable;
[theArrayVariable release];

theArrayVariable = [[NSMutableArray alloc] init];
arrayVariable = theArrayVariable;
[theArrayVariable release];

..., , , self.

?

+3
3

, , alloc, new copy. Apple , , ( "create" , retain to ) , release autorelease.

, self, setter ( , RHS ). , @property (, retain, retain release ).

NSMutableArray self, , , retain, 2, alloc -ed, release 1.

, releasing 1 release. ? , alloc -ed, , ivar , theArrayVariable. , theArrayVariable release, , , 0. , theArrayVariable .

+3

a) objective-c , , . , , . , . .

b) , . arrayVariable, , [[NSMutableArray alloc] init]; (, @property), -dealloc. , , .

obj-c: http://memo.tv/memory_management_with_objective_c_cocoa_iphone

+1

a) , , , .

UIImage *storedImage = [someFunction returnImage];
someFunction returns an image object to you, but it does not guarantee that the image object will live forever. If you do not want the image to be freed without your permission in a future time, you should own it by calling retain like this:
UIImage *storedImage = [[someFunction returnImage] retain];

, someFunction, . , , . someFunction, release , (, ).

,

UIImage *storedImage = [[UIImage alloc] init];

, storedImage, someFunction. , - .

+1
source

Source: https://habr.com/ru/post/1779790/


All Articles