discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Question about object initialization and autorelease


From: Lucas Schnorr
Subject: Re: Question about object initialization and autorelease
Date: Sat, 31 Mar 2012 01:38:26 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:11.0) Gecko/20120310 Thunderbird/11.0

Hi Omar,

On 03/30/2012 10:35 PM, Omar Campos wrote:
I've been reading the tutorials at http://www.gnustep.it/nicola/Tutorials/ and trying them out from memory, but using ProjectCenter instead of manually creating the GNUMakefile. When running my app, I kept getting an illegal operation. Upon closer inspection, I found the problem to be the following line:

mainWnd = AUTORELEASE([NSWindow alloc]);

mainWnd is a NSWindow* object declared in my AppController.h. When called from applicationWillFinishLaunching:, the line above caused an illegal operation. After removing the call to the AUTORELEASE() macro, it worked. Calling AUTORELEASE worked fine when adding an NSMenu* object to the app. The question is: when should I initialize an object with AUTORELEASE, and when shouldn't I?

Also, coming from a iOS background, I am used to seeing [[objectClass alloc] init] for initialization, yet in GNUStep, it is done with [new]. What's the difference? Or when should I use [[alloc] init]?


You can refer to this site for more information about this:
http://www.gnustep.org/resources/documentation/Developer/Base/ProgrammingManual/manual_3.html

My golden rule to work with retain/releases is to retain an object you do need, and release it when you no longer need. When you no longer need an object, but you are not sure if some other object will need it, you put that object in the latest created autorelease pool by calling [obj autorelease]. If no one calls a retain on that object up to the end of the next application loop (or the clean-up of the autorelease pool), the object is freed.

But in the link above you have a much better explanation of all the retain/release process, including the doubts
about the allocation you mentioned.

Cheers,
Lucas



reply via email to

[Prev in Thread] Current Thread [Next in Thread]