I have just finished reading the Memory Management Programming Guide for Cocoa. 
I am also working my way through the Hillegass book on Cocoa. There is 
something that is puzzling me that probably involves autorelease pools. Or else 
I am hopelessly confused in some other way.

I have created a trivial single-document Cocoa app. The application has a 
custom app controller class of my creation, called AppController. To keep this 
simple, there are no controls in the main window, and no actions or outlets in 
the AppController class. In other words, for purposes of this question the 
application does not actually do anything. However I have placed an 
AppController object in the application's MainMenu.xib doc window.

The AppController has an instance variable called instanceMadeObj. It is of 
type MadeObj* (another trivial custom class of my creation). The MadeObj class 
provides a "convenience constructor". In the init method of the AppController 
class, I initialize this instance variable by means of the convenience 
constructor of MadeObj. This convenience constructor autoreleases the object 
before returning it. (As I understand it that is what convenience constructors 
must do.)

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>

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.

I don't understand this. When is the autorelease pool created/released? I would 
guess that the AppController instance is created outside the main event loop. 
If the answer is in the Memory Management Programming Guide, it is not obvious 
to me. I did not expect that I should autorelease the instance variable in this 
case.

Where would I find out about this? Do I need to understand NSApplication 
better? Or maybe NIB handling? Point me to a manual, please. 

Any help is appreciated. I am still pretty much a Cocoa/obj-c novice.

Best,

- Phil -_______________________________________________

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