Source.isDestroyed

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.

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.

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;
}

Calls to this function from a thread other than the one acquired by the glib.MainContext the glib.Source is attached to are typically redundant, as the source could be destroyed immediately after this function returns. However, once a source is destroyed it cannot be un-destroyed, so this function can be used for opportunistic checks from any thread.

class Source
bool
isDestroyed
()

Return Value

Type: bool

TRUE if the source has been destroyed

Meta

Since

2.12