GLib.Source.prototype.is_destroyed
function is_destroyed(): Boolean { // Gjs wrapper for g_source_is_destroyed() }
Returns whether 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.
|[<!-- language="C" --> 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. A solution, to this particular problem, is to check to if the source has already been destroy within the callback.
|[<!-- language="C" --> 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; } ]|
Since 2.12
- Returns
true if the source has been destroyed