Re: g_object_add_toggle_ref



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.



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