Use autorelease when setting save property using dot syntax?

In some code examples, I use autorelease . I am not familiar with examples when required. For example, if I create an annotation object

Header file

 @interface someViewController: UIViewController { Annotation *annotation; } @property (nonatomic, retain) Annotation *annotation; @end 

Implementation file

 @implementation someViewController @synthesize annotation @end 

Question: Is this correct if I initialize the annotation object in the implementation file as follows?

 self.annotation = [[Annotation alloc] initWithCoordinate:location]; 

Do I need to install auto-advertising for this? Or can I just do it the usual way and add the release in the dealloc method?

+6
source share
2 answers

it is right:

self.annotation = [[[Annotation alloc] initWithCoordinate:location] autorelease];

since the annotation property is declared as a hold property, therefore assigning it increases the save counter.

you will also need, however, to release self.annotation in -dealloc .

:

  • init will set the hold value to 1;

  • assigning self.annotation will set it to 2;

  • autorelease will return it to 1 when the main loop is executed again;

  • a release in dealloc will set the hold value to 0 so that the object is freed);

the best way to think about autorelease is this: in my opinion: autorelease will “plan” an “automatic” release for your object at some (nearest) point in the future (usually when the flow control returns to the main loop, but the details are hidden in the hands Apple).

autorelease is mainly useful in combination with init , in particular in the following cases:

  • when you init local variable, so you do not need to release explicitly before it leaves the scope (the main loop will do this for you);

  • when you return a pointer to the object that you just created without retaining ownership of it (a typical case of create/make* selectors, the recipient needs to retain it to obtain ownership);

  • with properties that retain when you assign them an object to which they must belong uniquely;

  • with data structures that increase the retention count ( NSMutableArray , NSMutableDictionary , etc.): usually you should autorelease add a new init ed object when adding it to such a data structure.

except in case 2, it is obvious that the use of autorelease intended to improve the readability of the code and reduce the likelihood of errors (which means that in all other cases you could just release explicitly specify the object after the assignment or at the end of the area).

when using properties, you should always check whether they relate to the case of retain or assign / copy ; in the first case, assigning an init ed object to a property usually requires autorelease .

In any case, I would suggest at least removing one of the many memory management guides for iOS .

+15
source

Autorelease reports that the object is freed before leaving the area.

Sometimes, when you code, you will come across something like this

 - (void)doSomething { if(true) { NSString *foo = [[NSString alloc] initWithString:@"foo"]; //Some execution here [foo release]; } } - (void)doSomething { if(true) { //By doing this is telling to to release foo object before getting out of the scope //which is similar with above practice NSString *foo = [[[NSString alloc] initWithString:@"foo"] autorelease]; //Or you can do it this way NSString *foo = [[NSString alloc] initWithString:@"foo"]; [foo autorelease]; //Some execution carry on, it'll release foo before entering next scope } 

// This is beyond the scope}

Of course, releasing an object does not mean releasing the object. Sometimes you save an object so that you can still use it outside your scope.

Judging by your question, if your object is in your header / interface file. You must let it go in the dealloc method. CMIIW.

+2
source

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


All Articles