discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Introduce me to NSTableView


From: Nicola Pero
Subject: Re: Introduce me to NSTableView
Date: Fri, 6 Jun 2003 16:45:46 +0100 (BST)

> > True - but NSColorList-test contains a single-column NSBrowser. :-)
> 
> That proved a fine example and I've already implemented the categories 
> selection as an NSBrowser instead of a NSTableView. It seems a much 
> simpler solution.
> 
> As a general rule of thumb, should I call [... release] for every GUI 
> element after I add it to a container?

I would say the general rule of thumb if you should RELEASE objects you no
longer need.  After you put the object in the container, you no longer
need it yourself - obviously the container will, but that's his own
problem (he will retain it to make sure the object does not disappear when
you release it).

That is a common pattern - if you create an object (without autoreleasing
it), it has a retain count of 1, which keepts it alive.

You manage it, then hand it over to something else (for example, you put
it into a container).  Then you no longer need it yourself, so you
decrease the retain count by RELEASing the object.

If you hand't put it into a container, the object would be immediately
freed.  Because you've put it into a container, and the container RETAINs
object when they are added to it, the object will be alive until the
container RELEASEs it.  You have effectively given away any ownership of
the object, while the container has taken up ownership of it.  The object
will now be freed when the container no longer needs it.


Another (different) pattern is to create the object, then autorelease it.  
That means the retain count of the object is 1, but will be decremented
when the current/local autorelease pool is emptied (which might happen
when you exist your local context, or some context in a caller, or at the
next run loop iteration).  So you can use safely in the current context,
and it will be destroyed at some point after you exit the current context.

If you do that, when you are done using the object, you don't need to
RELEASE it, because the autorelease mechanism will already do an
equivalent thing automatically.


Ok - if that sounds confusing (I've tried to be simple but I might have
been more confusing than helpful!), try having a look at documentation on
the memory management in OpenStep/GNUstep/Cocoa.  Once you feel more
confident with that mechanism, the only thing you need to know is that 'a
container is retaining a view when you add the view to the container'.  
That's similar to what an array and other similar objects would do, so it
shouldn't be a surprise.





reply via email to

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