/*
 * Compile with: gcc signal-disconnect.c -o signal-disconnect `pkg-config --libs --cflags gtk+-3.0 gobject-2.0`
 * Valgrind: G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --log-file=vgdump ./signal-disconnect
 */
#include <unistd.h>

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

static void
button_clicked_cb (GtkWidget *widget, gpointer user_data)
{
	printf ("Button clicked\n");
}

void main (int argc, char **argv)
{
	gtk_init (&argc, &argv);
	GtkWidget *button = gtk_button_new ();
	gint sid = g_signal_connect (button, "clicked", (GCallback) button_clicked_cb, NULL);
	printf ("Is connected: %i\n", g_signal_handler_is_connected (button, sid));
	g_signal_handler_disconnect (button, sid);
	printf ("Is connected: %i\n", g_signal_handler_is_connected (button, sid));
	gtk_widget_destroy (button);

	//create same widget but don't disconnect (use gtk_widget_destroy() instead)
	sid = -1;
	button = NULL;
	button = gtk_button_new ();
	sid = g_signal_connect (button, "clicked", (GCallback) button_clicked_cb, NULL);
	printf ("Is connected: %i\n", g_signal_handler_is_connected (button, sid));
	gtk_widget_destroy (button);
	printf ("Is connected: %i\n", g_signal_handler_is_connected (button, sid));
}
