LifeTime of an autoreleased object
-(void)returnString
{
NSString *str=[[NSString alloc]init];
return [str autorelease];
}
-(void)getString
{
NSString *p=[self returnString];
}
Here What is the lifetime of string str. When will the retainCount of str will become zero. I know that its retain count will become zer开发者_StackOverflowo at the end of a runloop. Does the end of the runloop means the end of a function which is calling returnString. If not what a runloop means and what does the end of a runloop means.
-(void)returnString
{
NSString *str=[[NSString alloc]init];
return str;
}
-(void)getString
{
NSString *p=[self returnString];
[p release];
}
Is this type of releasing is correct or not?
Run loop in iOS wait for some events to occur and acts upon it. A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.
You need to create the Autorelease pool in your methods if not it will only create at the start of the Event Loop and Drained at the end of the Event Loop.
Check this. End of run loop -- autorelease pool recovery
When you autorelease
an object, it's added to the autorelease pool and queued for release. When the autorelease pool is released, all of the objects in the pool are sent a release
message and they are released from memory. To answer your question, the object stays in memory for as long as the pool does, assuming it's not retained at some point.
More information on this can be found at the Autorelease Pools section of the Memory Management Programming Guide.
精彩评论