Package: glib

Function g-source-is-destroyed

Lambda List

g-source-is-destroyed (source)

Arguments

source -- a g-source instance

Return Value

True if source has been destroyed.

Details

Returns whether the source has been destroyed. This is important when you operate upon your objects from within idle handlers, but may have freed the object before the dispatch of your idle handler.
  static gboolean
  idle_callback (gpointer data)
  {
    SomeWidget *self = data;

GDK_THREADS_ENTER (); /* do stuff with self */ self->idle_id = 0; GDK_THREADS_LEAVE ();

return G_SOURCE_REMOVE; }

static void some_widget_do_stuff_later (SomeWidget *self) { self->idle_id = g_idle_add (idle_callback, self); }

static void some_widget_finalize (GObject *object) { SomeWidget *self = SOME_WIDGET (object);

if (self->idle_id) g_source_remove (self->idle_id);

G_OBJECT_CLASS (parent_class)->finalize (object); }
This will fail in a multi-threaded application if the widget is destroyed before the idle handler fires due to the use after free in the callback function. A solution, to this particular problem, is to check to if the source has already been destroy within the callback function.
  static gboolean
  idle_callback (gpointer data)
  {
    SomeWidget *self = data;

GDK_THREADS_ENTER (); if (!g_source_is_destroyed (g_main_current_source ())) { /* do stuff with self */ } GDK_THREADS_LEAVE ();

return FALSE; }
 

See also

2021-12-10