Re: Gtk-- refcounting question
- From: Owen Taylor <owt1 cornell edu>
- To: gtk-list redhat com
- Subject: Re: Gtk-- refcounting question
- Date: 05 Mar 1998 17:35:07 -0500
Sebastian Wilhelmi <wilhelmi@ira.uka.de> writes:
> Hi, here are some of my thoughts on this issue..
>
> How long do you expect a C++-object to live?
> yes, from construction till destruction, and everything other than that
> would lead to huge problems.
>
> So I think, the best and most natural way would be the following:
>
> In the constructor you have to call gtk_*_ref( .... );
> and in the destructor gtk_*_destroy( .... );
>
> so, you could say (which of course is true), that the nifty
> ref-counting-stuff of gtk is completely wasted now, and: yes, you're
> right. But to add it to C++, just because it is there in C can't be the
> answer.
For objects where the C++ is just a wrapper (such as styles), this
approach works, as long as you don't want to refcount them in C++.
If you do want to refcount them in C++ (and refcounting is a
reasonable way of doing memory-management in C++), then the
correct approach is, I think, doing this, and refcounting the
wrapper.
But for Gtk_Objects, which can be _implemented_ in C++ (as when
you derive a new class and override methods) this doesn't work.
The C++ object may need to persist when nobody knows about it
from C++ at all.
Luckily, GtkObjects also are a lot more full-featured than other Gtk
"objects" - you can do weak references using gtk_object_set_data_full,
and thus, since you are notified when the GTK object is destroyed,
simply use the GTK refcounting to refcount your C++ object as well.
(See my other post for details)
> But never do:
>
> delete( this );
>
> That IS evil.
I'll take your word on it. (I'm no C++ expert)
I think the right way to handle this is to make unref a static operation.
Gtk_Object::unref(object);
(For unref, you can actually overload unref and use the same operation
for _every_ gtk-- object)
gmm_unref(gtk_object)
gmm_unref(gtk_style)
gmm_unref(gdk_window)
<aside>
CORBA also makes the ref() equivalent, _duplicate() a static member
function to allow the same operation to be used on non-reference
counted objects - you have something like:
CORBA::Foo *CORBA::Foo::_duplicate(CORBA::Foo &_obj)
So it can either add a reference count to Foo, or copy it as
desired. But if you are satisfied with just reference counting,
a non-static Foo::ref() will work fine.
</aside> )
> (1) because of that, the destructor of Gtk_Object should certainly
> check, if the ref-count of its contained gtkobjekt is already 0 and then
> not call gtk_object_destroy..
If you check, and gtkobject->refcount == 0, then you are in big
trouble, because gtkobject has already been freed. ;-) As long as you
hold a pointer to a GtkObject, you must also hold a reference count.
(Also, gtk_object_destroy has "nothing" to do with memory management
any more - it merely destroys the onscreen representation of the
widget)
Regards,
Owen
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]