g_object_add_toggle_ref

classic Classic list List threaded Threaded
3 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
Loading...