Re: Sinkability: usefull in language bindings ? [was: Re: Sinkability considered harmful]



Tristan Van Berkom said:
> muppet wrote:
>> Tristan Van Berkom said:
>>
>>>muppet,
>>>     GtkObject is a special case, it means that in your
>>>binding you have to distinguish it from a GObject and treat
>>>it differently, if there were no floating flag; you wouln't
>>>need to do this.
>>
>>
>> This is indeed the situation -- the binding treats GtkObjects differently
>> from
>> GObjects.  What i was trying to say was that the floating reference allowed
>> us
>> to treat all GtkObject returns identically, while we must pay very close
>> attention to non-GtkObject returns to get ownership correct.
>
> I am curious as to how that is; for example:
>
>      gtk_range_get_adjustment() returns a private pointer
>      to an adjustment, one would have to ref it if one wanted
>      to keep it around after.
>
>      g_object_new (GTK_TYPE_ADJUSTEMENT, NULL) returns a
>      floating GtkAdjustment.
>
> The bottom line is that, making the assumption that functions
> returning "already sunk" GtkObjects will never give you an extra
> reference, or that functions returning "floating" GtkObjects
> will always have a reference count of 1 is wrong and dangerous.

An important pieces of information is that the wrapper object holds a
reference on the C object.  The wrapper object is, itself, reference-counted,
and a delicate dance keeps them both alive for the right amount of time.  We
must explicitly point out when a wrapper is to be the sole owner of a normal
GObject, but for GtkObjects we can unconditionally ref and sink and it all
just works.


For a plain GObject:

   $pixbuf = Gtk2::Gdk::Pixbuf->new (...);

- C function returns object with one reference.
- wrapper adds one reference.
- binding knows that this function is a constructor, so it removes
  one reference to ensure that the wrapper is the sole owner.

and

   $pixbuf = $pixbuf_loader->get_pixbuf;  # you don't own the return yet!

- C function returns object with n refs.
- wrapper adds one reference.


Contrast this with a GtkObject:

   $button = Gtk2::Button->new ("hi");

breaks down into

- C function returns object with one floating ref
- wrapper refs and sinks, becoming sole owner.

Whereas:

   $child = $widget->get_child;

breaks down into

- C function returns sunken object with n refs.
- wrapper refs and sinks.  was already sunk, so sink does nothing, and
  C object gets another ref owned by the wrapper.



-- 
muppet <scott at asofyet dot org>




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