Skip to content

Instantly share code, notes, and snippets.

@mjakeman
Last active January 10, 2021 09:59
Show Gist options
  • Save mjakeman/441720c9a53520411d47d2ff43938393 to your computer and use it in GitHub Desktop.
Save mjakeman/441720c9a53520411d47d2ff43938393 to your computer and use it in GitHub Desktop.
// Destroy Notify Demo
// Adapted from GTK 3 Tutorial
//
// Compile with:
// gcc `pkg-config --cflags gtk+-3.0` -o destroy-notify destroy-notify.c `pkg-config --libs gtk+-3.0`
//
// Output:
// $ ./destroy-notify
// Source Func
// Destroy Notify
//
// This demonstrates that by returning FALSE from the function we remove
// the source and therefore call destroy notify to do our cleanup.
#include <gtk/gtk.h>
static void
activate (GtkApplication* app,
gpointer user_data)
{
// Just to put something on the screen
GtkWidget *window = gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW (window), "Window");
gtk_window_set_default_size (GTK_WINDOW (window), 200, 200);
gtk_widget_show_all (window);
}
static gboolean
source_func (gpointer user_data)
{
// We print out "Source Func" to the console,
// then remove the source by returning FALSE.
// This will trigger destroy_notify for us.
g_print("Source Func\n");
return FALSE;
}
static void
destroy_notify (gpointer user_data)
{
// If the source is removed successfully, destroy
// notify is called. We can free resources here.
g_print("Destroy Notify\n");
}
int
main (int argc,
char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("org.gtk.example", G_APPLICATION_FLAGS_NONE);
g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
// Add asynchronous timeout function to main loop
g_timeout_add_full (0, 1000, source_func, NULL, destroy_notify);
status = g_application_run (G_APPLICATION (app), argc, argv);
g_object_unref (app);
return status;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment