Re: custom gtkwidget destruction



On 24 February 2011 22:57, James Morris <jwm art net gmail com> wrote:
I am wondering if in this case, as there is no extra data to destroy,
is it really necessary for me to specify the destroy function?

Put another way, by specifying the destroy function, am I duplicating
by 'over-riding' the 'default destructor' and/or possibly omitting
things which should be done?

You only need to define destroy() if you want to do something on
destroy. It can be handy for debugging though, I often have empty
virtual methods with #ifdef DEBUG printfs in.

You chain up to your parent classes destroy function, so nothing is
being omitted, don't worry.

With regard to the docs, finalize and dispose are mentioned. Are these
only required if I explicitly need deeper control over the destruction
(of - in this case - the widgets my widget uses)?

They all have slightly different meanings. My understanding is:

  destroy

This means: every other object holding a ref to me should drop it.
It's used to tell OTHER objects that you are going, not to do any
destruction yourself.

For example, when you can call gtk_widget_destroy() on a menu item, it
does not directly destroy the item. Instead, the items emits a
"destroy" signal, the enclosing menu sees that and drops the ref it
holds to the item, the item's refcount hits zero, and the item is then
dispose()d.

So ... "destroy" is generally something you listen out for for any
objects you hold references to, not something you implement yourself.

  dispose

This is called when an object's refcount hits zero. This is where you
should drop any refs you hold to other objects. Note that dispose can
run several times, since objects can have refcounts that go back up
again from zero, so be careful not to double-free anything.

  finalize

This is called just before the object's memory is freed. It is called
exactly once and is a good place to free any strings or other small
structures the object holds.

John



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