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: Eric Wasylishen
Subject: Re: Question about object initialization and autorelease
Date: Fri, 30 Mar 2012 15:16:55 -0600

Hi Omar,

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?

AUTORELEASE doesn't initialize an object; AUTORELEASE(x) is the same as [x autorelease] - so you don't want to send the -autorelease message to an uninitialized NSWindow. What you probably want on that line is to call the NSWindow initializer, -initWithContentRect:styleMask:backing:defer:.

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

There is no difference; [x new] is the same as [[x alloc] init].

However, my suggestion is to stick to [x autorelease] and [[x alloc] init] instead of AUTORELEASE(x) or [x new].

Hope this helps,

Eric

reply via email to

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