Re: [gtk-list] Re: Gtk-- refcounting question



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. 

So there are of course some problems remaining.

There has to be a way to just add a Gtk_Object to a Gtk_Container, such
that the control over this Gtk_Object is transferred to the
Gtk_Container, i.e. it is automatically deleted, when not needed
anymore.

There would have to be a helper class, that is a Gtk_Object itself and
that has to be constructed with a pointer to another Gtk_Object, that it
then takes control over, just like the auto_ptr, so only
Gtk_Object_Auto_Ptr( new( Gtk_Something ) ) would be allowed, not
Gtk_Object_Auto_Ptr( &Gtk_Something_Instance ). This
Gtk_Object_Auto_Ptr-contructor would than set the GtkDestroyNotify
function with gtk_object_set_data_full() and on destruction it would
just unref the original object one time, thereby preventing the whole
thing to be deleted, as long as itself is living. So it would look
something like this:

class Gtk_Object_Auto_Ptr : Gtk_Object
{
  Gtk_Object *CPPObject;
  static void DoDeletion( gpointer Object )
    {
      delete ((Gtk_Object*)Object); // (1)
    }
public:
  Gtk_Object_Auto_Ptr( Gtk_Object* Object ) : CPPObject( Object )
    {
      gtkobject = Object->gtkobject;  // So I behave like Object.
    }
  ~Gtk_Object_Auto_Ptr()
    {
       gtk_object_set_data_full( Object->gtkobject, ...., 
                                 (gpointer) CPPObject, &DoDeletion );
       gtk_object_unref( Object->gtkobject ); 
       // this makes the ref in the contructor of Gtk_Object undone
    }
}

But never do:

    delete( this );

That IS evil.

(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..

Greeting
Sebastian
-- 
+--------------============##################============--------------+
| Sebastian Wilhelmi, Institut fuer Rechnerentwurf und Fehlertoleranz, |
| Universitaet Karlsruhe;  Gebaeude 20.20, Raum 263, D-76128 Karlsruhe |
| mail: wilhelmi@ira.uka.de; fax: +49 721 370455; fon: +49 721 6084353 |
+----------------> http://goethe.ira.uka.de/~wilhelmi <----------------+



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