Okay say I have a function like below:
-(NSNumber *)calculate{
NSNumber *myNum = [[[NSNumber alloc]initWithInt:5] autorelease];
return myNum;
}
When will myNum be released? Will whenever I call calculate, myNum will be created and added to the stack?
Also say I have a property like:
@property (nonatomic, retain) NSMutableArray *inputsArr;
and I synthesized it as:
@synthesize inputsArr = _inputsArr;
and I alloc and initiate it in the code of one of mu functions..
How would I go about releasing this memory? any guides to CoaCoa memory management…I can only find really confusing or obvious guides..
Thanks in advance
autorelease pools are thread local stacks — you push and pop them. the deferred
releasemessage will be sent to the object when the pool is destroyed.consider this:
So ‘when’ really depends on how the autorelease pools are built — but it’s always possible to ensure your objects outlive a local pool, so this is never a restriction:
The above is what you should rely on. In some real world cases, an object may still be alive where you would expect “BAM!”, but you should never rely on “well, it should have been destroyed, but it seems to work alright”.