discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Tutorials enhancement proposal : First Steps in GNUstep GUI Programm


From: David Chisnall
Subject: Re: Tutorials enhancement proposal : First Steps in GNUstep GUI Programming (2) : NSWindow, NSButton
Date: Wed, 10 Jun 2020 11:44:06 +0100
User-agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0

On 10/06/2020 10:42, Patrick Cardona via Discussion list for the GNUstep programming environment wrote:
Ref : First Steps in GNUstep GUI Programming (2) : NSWindow,  NSButton
Edition : First Edition: July 2000; Last Updated: January 2008
URL : http://www.gnustep.it/nicola/Tutorials/WindowsAndButtons/index.html

Hi Nicola (Pero),
Hi All,

I wish first to thank You, Nicola, for the great tutorials You made.
They helped Me to understand and begin my learning pretty well.

Trying to play and understand the final code in the Tutorial 'First Steps in GNUstep GUI Programming (2) : NSWindow,  NSButton', I was facing to some warnings and bugs. I think You should modify the final code listing at page 9, in the '@implementation' section like this :

(1) In the method 'dealloc' :

- (void) dealloc
{
   RELEASE (myWindow);
   /* According to a warning while making the app */
   [super dealloc];
}

Note that this -dealloc method is not required at all with ARC.

(2) In the method 'createWindow' :

- (void) createWindow
{
...
myWindow = [NSWindow alloc];

   /* Don't make a assignment of 'myWindow' again...
   myWindow = */
   /* So I kept only the message... */
   [myWindow initWithContentRect: rect
                        styleMask: styleMask
                        backing: NSBackingStoreBuffered
                        defer: NO];

  /* Here, the contentView was obviously missing : I added it... */
[myWindow setContentView: myButton];
...
}

As Richard says, the semantics of init-family methods mean that this doesn't do what you think it does.

An init-family method consumes the receiver and returns a new owning reference to the return value. In the common case, the the receiver will be the same as the return value and so this cancels out. In the case of things like NSWindow, it is quite likely that they are different. NSWindow +alloc is likely to return a singleton placeholder and do the real allocation when you call one of the init-family methods that

That is why it is considered good style to never assign the result of alloc to a variable and always chain the call as [[SomeClass alloc] initWithSomeInitFamilyMethod: ...]. For more information about method families, see this:

https://www.informit.com/articles/article.aspx?p=1745875&seqNum=4

Note that without ARC, your version may generate memory management errors. With ARC, it will just have surprising behaviour: the newly created window created by the init-family method will be deallocated immediately and the value assigned to myWindow will point to the placeholder.

I Hope this could be helpful for other Beginners like Me. ;-)

I would *strongly* recommend any beginner use ARC from the outset. Thinking in terms of ownership and consume operations is a far better memory model than thinking in terms of reference count manipulation.

ARC also makes memory management work cleanly in Objective-C++, so things like std::vector<NSMutableString*> work correctly (and will typically be faster than NSMutableArray<NSMutableString*>).

David




reply via email to

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