When will NSautorelease release it?

Ok, I have a function as shown below:

-(NSNumber *)calculate{ NSNumber *myNum = [[[NSNumber alloc]initWithInt:5] autorelease]; return myNum; } 

When will myNum be released? Will whenever my call to computation myNum be created and added to the stack?

They also say that I have a property like:

 @property (nonatomic, retain) NSMutableArray *inputsArr; 

and I synthesized it as:

 @synthesize inputsArr = _inputsArr; 

and I select and run it in the code of one of the mu functions. How can I free this memory? any CoaCoa memory management guides ... I can find really confusing or obvious guides. thanks in advance

+4
source share
2 answers

autorelease pools are streaming local stacks - you click and pop up. a delayed release message will be sent to the object when the pool is destroyed.

consider this:

 NSAutoreleasePool * pool = [NSAutoreleasePool new]; NSNumber * n = [NSNumber numberWithDouble:1.0/17.0]; << n is autoreleased [n self]; << OK! [pool release]; << n is messaged release [n self]; << BAM! 

So, when โ€œreallyโ€ depends on how autocomplete pools are created, but you can always ensure that your objects survive the local pool, so this is never a limitation:

 NSAutoreleasePool * pool = [NSAutoreleasePool new]; NSNumber * n = [NSNumber numberWithDouble:1.0/17.0]; << n is autoreleased [n self]; << OK! [n retain]; [pool release]; << n is messaged release [n self]; << OK! [n release]; [n self]; << BAM! 

The above is what you must rely on. In some real cases, the object may still be alive where you expect โ€œBAM!โ€, But you should never rely on โ€œwell, it should have been destroyed, but everything seems to be in order.โ€

+1
source

The NSApplication class NSApplication up completion pools (instances of the NSAutoreleasePool class) during initialization and inside the event loop, in particular, within its initialization methods (or sharedApplication ) and run . usually the autoresist pool is set at the end of the event loop, but it is up to you or the application.
If you intend to use many temporary objects (auto-implemented / from convenient methods), you can consider creating your own short-term auto-detection pools to avoid short-term memory peaks. Autorelease objects are added to the last autocomplete pool before being created.

  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; // create your own little autorelease pool // these objects get added to the autorelease pool you created above NSNumber *aNumber1 = [NSNumber numberWithFloat:1]; // refcount is 1, you are not owner, will be automatically released NSNumber *aNumber2 = [NSNumber numberWithFloat:2]; // refcount is 1, you are not owner, will be automatically released NSNumber *aNumber3 = [NSNumber numberWithFloat:3]; // refcount is 1, you are not owner, will be automatically released NSNumber *aNumber4 = [NSNumber numberWithFloat:4]; // refcount is 1, you are not owner, will be automatically released NSNumber *aNumber5 = [NSNumber numberWithFloat:5]; // refcount is 1, you are not owner, will be automatically released NSNumber *aNumber6 = [NSNumber numberWithFloat:6]; // refcount is 1, you are not owner, will be automatically released // ... do a bunch of stuff with all objects above. ... [pool release]; // all objects added to this pool (the ones above) are released 

See Memory Management with Objective C / Cocoa / iPhone .

+1
source

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


All Articles