diff --git a/glib/gmain.c b/glib/gmain.c index af0a29ae5..7a5ec1f45 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -2112,11 +2112,15 @@ g_source_unref_internal (GSource *source, if (source->source_funcs->finalize) { + /* Temporarily increase the ref count again so that GSource methods + * can be called from finalize(). */ + source->ref_count++; if (context) UNLOCK_CONTEXT (context); source->source_funcs->finalize (source); if (context) LOCK_CONTEXT (context); + source->ref_count--; } g_free (source->name); diff --git a/glib/gmain.h b/glib/gmain.h index e8cdeed7a..26400d44b 100644 --- a/glib/gmain.h +++ b/glib/gmain.h @@ -104,7 +104,10 @@ typedef struct _GSourceCallbackFuncs GSourceCallbackFuncs; * are needed for this type of event source. The return value of the * @dispatch function should be #G_SOURCE_REMOVE if the source should be * removed or #G_SOURCE_CONTINUE to keep it. - * @finalize: Called when the source is finalized. + * @finalize: Called when the source is finalized. At this point, the source + * will have been destroyed, had its callback cleared, and have been removed + * from its #GMainContext, but it will still have its final reference count; + * so methods can be called on it from within this function. * * The `GSourceFuncs` struct contains a table of * functions used to handle event sources in a generic manner.