diff --git a/glib/gmain.c b/glib/gmain.c index af979c8b8..a9a287d6c 100644 --- a/glib/gmain.c +++ b/glib/gmain.c @@ -969,13 +969,17 @@ g_source_iter_next (GSourceIter *iter, GSource **source) * GSourceList to be removed from source_lists (if iter->source is * the only source in its list, and it is destroyed), so we have to * keep it reffed until after we advance iter->current_list, above. + * + * Also we first have to ref the next source before unreffing the + * previous one as unreffing the previous source can potentially + * free the next one. */ + if (next_source && iter->may_modify) + g_source_ref (next_source); if (iter->source && iter->may_modify) g_source_unref_internal (iter->source, iter->context, TRUE); iter->source = next_source; - if (iter->source && iter->may_modify) - g_source_ref (iter->source); *source = iter->source; return *source != NULL;