g_object_add_toggle_ref

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

g_object_add_toggle_ref

Stefan Salewski-2
My high level Nim wrapper (https://github.com/StefanSalewski/nim-gi2)
is indeed basically working.

Currently I am using g_object_add_toggle_ref() and
g_object_set_qdata(). The first for the garbage collection tasks, and
the later for retrieving the proxy object from the associated GTK
GObject, for example when functions like gtk_widget_get_parent() are
used. That seems to work, but I have the feeling of some redundancy.
Both gobject functions are passed the address of my proxy object. But
g_object_add_toggle_ref() seems to offer no support to get the address
back, that is why I use additional g_object_set_qdata() which allows me
to get back the proxy address with g_object_get_qdata().


proc gtk_button_new*(): ptr Button00 {.
    importc: "gtk_button_new", libprag.}

proc newButton*(): Button =
  new(result, finalizeGObject)
  result.impl = gtk_button_new()
  GC_ref(result)
  g_object_add_toggle_ref(result.impl, toggleNotify, addr(result[]))
  assert(g_object_get_qdata(result.impl, Quark) == nil)
  g_object_set_qdata(result.impl, Quark, addr(result[]))
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_object_add_toggle_ref

Stefan Salewski-2
On Sat, 2017-06-03 at 15:37 +0200, Stefan Salewski wrote:

What is a bit strange is

void
g_object_add_toggle_ref (GObject       *object,
                         GToggleNotify  notify,
                         gpointer       data)
{
  ToggleRefStack *tstack;
  guint i;
  
  g_return_if_fail (G_IS_OBJECT (object));
  g_return_if_fail (notify != NULL);
  g_return_if_fail (object->ref_count >= 1);

  g_object_ref (object);

So after a call of g_object_add_toggle_ref() ref_count of an object is
always greater or equal to two? But how can now that value drop ever
again below 2? When a new references is added, it increases, and when
that references is removed, the value is decreased. But never below 2
this way? It is a bit confusing indeed.

More confusing is, that g_object_ref_sink () seems to exist since
gobject release 2.10, while g_object_add_toggle_ref () seems to exists
already since 2.8.

And, for my understanding of English wording, the documentation of
g_object_ref_sink() contradicts even itself:

https://developer.gnome.org/gobject/stable/gobject-The-Base-Object-Type
.html#g-object-ref-sink


"Increase the reference count of object
, and possibly remove the
floating reference, if object
 has a floating reference.

In other words, if the object is floating, then this call "assumes
ownership" of the floating reference, converting it to a normal
reference by clearing the floating flag while leaving the reference
count unchanged.  If the object is not floating, then this call
adds a new normal reference increasing the reference count by one."

I assume, that the second part is correct. Maybe the first sentence
should be something like "Increase the reference count of object
, OR possibly"

So it will be not that easy to get fully working Garbage Collector
support.

_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_object_add_toggle_ref

Stefan Salewski-2
On Tue, 2017-06-06 at 11:13 +0200, Stefan Salewski wrote:
> On Sat, 2017-06-03 at 15:37 +0200, Stefan Salewski wrote:
>
> What is a bit strange is
>

Well, at least my understanding so far seems to be correct:

// gcc -o test reftest.c `pkg-config --libs --cflags gtk+-3.0`

#include <gtk/gtk.h>
#include <gobject/gobject.h>
#include <glib.h>
#include <stdio.h>

void
toggleNotify (gpointer data, GObject *object, gboolean is_last_ref) {
  printf("toggleNotify\n");
}

int main(int argc, char *argv[]) {
  GtkWidget *window;
  GtkWidget *button;
  gtk_init(&argc, &argv);
  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  button = gtk_button_new();
  printf("refcount %d\n", ((GObject *) button)->ref_count);
  g_object_add_toggle_ref((GObject *) button, toggleNotify, NULL);
  printf("refcount %d\n", ((GObject *) button)->ref_count);
  g_object_unref(button);
  gtk_widget_show(window);
  g_signal_connect(window, "destroy",
      G_CALLBACK(gtk_main_quit), NULL);  
  gtk_main();
  return 0;
}

Output is

refcount 1
refcount 2
toggleNotify

so after call of g_object_add_toggle_ref() ref_count is indeed 2, and
after call of g_object_unref() the toggleNotify() callback is
activated.

_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_object_add_toggle_ref

Stefan Salewski-2
On Tue, 2017-06-06 at 14:33 +0200, Stefan Salewski wrote:
> so after call of g_object_add_toggle_ref() ref_count is indeed 2, and
> after call of g_object_unref() the toggleNotify() callback is
> activated.

Some more investigations seems to indicate:

When I create a widget and apply g_object_add_toggle_ref(), then ref
count is increased to 2 and the widget is still floating, When I now
add that widget to a container, object is sunk, ref count stays at 2,
and the toggle ref callback in not yet called. But when I remove the
widget from the container again, ref count decreases to 1, and the
toggle ref callback is called. This is fine, as the callback can now
decrease refcount of the proxy object as well. Adding that widget to
the container again works also, callback is called...

But there is one drawback: In GC languages the user is generally free
to create a large number of object and just forget about them. But when
the user creates widgets and never add them to a container, there state
remains ref count 2 floating, and they are keep alive until program
terminates.

Well, we may say that the user should not do that, because it makes no
sense.

A possible fix would be to fake a conainer add/remove operation. I can
do that by a ref_sink/unref function call on the widget after call of
g_object_add_toggle_ref(). That results in a ref count of 1 and a not
floating object, which behaves fine futher. But I am not sure if I
should do that fake, maybe it again generates other problems.


_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_object_add_toggle_ref

Emmanuele Bassi
Hi Stefan;

I very much doubt anybody here has knowledge of toggle reference and
language bindings.

You want [hidden email], or you want to join the #gtk+ IRC
channel on irc.gnome.org.

As a note, though:

> But when the user creates widgets and never add them to a container, there state
> remains ref count 2 floating

All GC language bindings will immediately sink the floating reference,
as it makes memory management harder for them (it's a C feature, after
all). So my suggestion is to immediately and automatically sink the
floating reference inside your wrapper around g_object_new() before it
returns the instance to the non-C side.

Ciao,
 Emmanuele.

On 25 June 2017 at 10:38, Stefan Salewski <[hidden email]> wrote:

> On Tue, 2017-06-06 at 14:33 +0200, Stefan Salewski wrote:
>> so after call of g_object_add_toggle_ref() ref_count is indeed 2, and
>> after call of g_object_unref() the toggleNotify() callback is
>> activated.
>
> Some more investigations seems to indicate:
>
> When I create a widget and apply g_object_add_toggle_ref(), then ref
> count is increased to 2 and the widget is still floating, When I now
> add that widget to a container, object is sunk, ref count stays at 2,
> and the toggle ref callback in not yet called. But when I remove the
> widget from the container again, ref count decreases to 1, and the
> toggle ref callback is called. This is fine, as the callback can now
> decrease refcount of the proxy object as well. Adding that widget to
> the container again works also, callback is called...
>
> But there is one drawback: In GC languages the user is generally free
> to create a large number of object and just forget about them. But when
> the user creates widgets and never add them to a container, there state
> remains ref count 2 floating, and they are keep alive until program
> terminates.
>
> Well, we may say that the user should not do that, because it makes no
> sense.
>
> A possible fix would be to fake a conainer add/remove operation. I can
> do that by a ref_sink/unref function call on the widget after call of
> g_object_add_toggle_ref(). That results in a ref count of 1 and a not
> floating object, which behaves fine futher. But I am not sure if I
> should do that fake, maybe it again generates other problems.
>
>
> _______________________________________________
> gtk-list mailing list
> [hidden email]
> https://mail.gnome.org/mailman/listinfo/gtk-list



--
https://www.bassi.io
[@] ebassi [@gmail.com]
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: g_object_add_toggle_ref

Stefan Salewski-2
On Sun, 2017-06-25 at 10:59 +0100, Emmanuele Bassi wrote:
> I very much doubt anybody here has knowledge of toggle reference and
> language bindings.

Yes, I guess that is true, the original authors seems all to be
retired. For IRC or devel list, I fear situation is not really better,
and I do not really want to make noise there, the few remaining
developers may have to do more important stuff.

> All GC language bindings will immediately sink the floating reference,
> as it makes memory management harder for them (it's a C feature, after
> all). So my suggestion is to immediately and automatically sink the
> floating reference inside your wrapper around g_object_new() before it
> returns the instance to the non-C side.

That is an interesting hint. Indeed I did that in my first draft about
a year ago, before I discovered  g_object_add_toggle_ref() function. Of
course it makes sense to use it in combination.

At least basically my Nim wrapper is working already -- maybe for
details I will have to look at wrappers for other languages. Maybe I
should consider contacting the Rust developers, as Rust is also a
compiled language, and they have some GTK bindings.
_______________________________________________
gtk-list mailing list
[hidden email]
https://mail.gnome.org/mailman/listinfo/gtk-list
Loading...