On Mar 8, 2010, at 7:44 PM, Philippe Sismondi wrote:

> Here is my AppController's init method, in which instanceObj is set to an 
> autoreleased MadeObj:
> 
> - (id)init
> {
>       if(![super init])
>               return nil;
>       
>       instanceMadeObj = [MadeObj madeObjConvenienceConstructor];
>       
>       return self;
> }
> 
> In the MadeObj class I included some NSLog statements that tell me when a 
> MadeObj instance is created and dealloc'd. As expected, when the application 
> starts an AppController object is instantiated, and thus also instanceMadeObj 
> is set to a newly instantiated MadeObj.
> 
> When I start the application, I get this in the console:
> 
> 2010-03-08 22:06:00.145 TestPool[2873:a0f] made obj with convenience 
> constructor: <MadeObj: 0x10012ee60>
> 2010-03-08 22:06:00.168 TestPool[2873:a0f] deallocating MadeObj: <MadeObj: 
> 0x10012ee60>

This is fully expected behavior. When you use your 
+madeObjConvenienceConstructor method, it returns an object that you do not 
own, as such it may be deallocated at anytime. When you return to the runloop 
and the autorelease pool is drained, the last reference to the object is 
released and your object is deallocated.

> In other words, the MadeObj instance for AppController's instance variable is 
> created and then immediately dealloc'd. I take this to mean that the 
> autorelease pool it was in was released/drained. If I change the 
> AppController init method to retain instanceMadeObj, the dealloc does not 
> happen.

When you use +alloc/-init on an object however, you are returned an object that 
you own, thus it will not be deallocated until after you call -release on that 
object (how soon after will depend on what other objects also own the object).

While your question was sparked by the behavior of an auto release pool, it is 
really all about object ownership. Because in your example above you don't own 
the object that you are referencing, it may be deallocated at any time, leaving 
you with a dangling reference. When you switch to +alloc/-init you instead get 
an object that you do own, which won't be deallocated until you do release it.

If this is still confusing, I would recommend that you continue taking a look 
at the Memory Management guide until you've internalized this. It also 
generally helps to not think about autorelease pools as anything more than an 
implementation detail until you have gotten all of this down, as they won't 
make a whole lot of sense to you until you understand how object ownership 
works.
--
David Duncan
Apple DTS Animation and Printing

_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to