« Return to Thread: Question about object initialization and autorelease

Re: Question about object initialization and autorelease

by David Chisnall :: Rate this Message:

| View in Thread

On 30 Mar 2012, at 21:35, Omar Campos wrote:

> mainWnd = AUTORELEASE([NSWindow alloc]);

AUTORELEASE is a legacy macro for GC support from before the compiler was able to elide autorelease messages in GC mode.  It is equivalent to sending an autorelease message.  This code is therefore the same as:

mainWnd = [[NSWindow alloc] autorelease];

This may work, unless NSWindow returns a singleton or similar, but I wouldn't expect it to..  

> 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?

You don't initialise an object with AUTORELEASE, you autorelease an object with AUTORELEASE.  The correct time to use AUTORELEASE is between about 1995 and 2009.  The correct time to send an -autorelease message is when you want a non-owning reference that is guaranteed to persist for the duration of scope of a local variable and you have a religious of philosophical aversion to ARC.

> Also, coming from a iOS background,

If you come from iOS, is there a reason you choose not to use ARC?

> 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]?

new has exactly the same behaviour in GNUstep as it does on OPENSTEP, OS X and iOS:

+ (id)new
{
        return [[self alloc] init];
}

David
-- Sent from my brain


_______________________________________________
Discuss-gnustep mailing list
Discuss-gnustep@...
https://lists.gnu.org/mailman/listinfo/discuss-gnustep

 « Return to Thread: Question about object initialization and autorelease