After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 479787 - Calling "g_main_context_pending" increases reference count for the ready sources
Calling "g_main_context_pending" increases reference count for the ready sources
Product: glib
Classification: Platform
Component: mainloop
Other Linux
: Normal normal
: ---
Assigned To: gtkdev
Depends on:
Reported: 2007-09-24 12:15 UTC by Areg Beketovski
Modified: 2018-05-24 11:06 UTC
See Also:
GNOME target: ---
GNOME version: ---

The test case reproducing the bug (3.09 KB, text/plain)
2007-09-24 12:17 UTC, Areg Beketovski
proposed fix (3.25 KB, patch)
2016-08-16 14:19 UTC, Namhyung Kim
none Details | Review

Description Areg Beketovski 2007-09-24 12:15:55 UTC
Please describe the problem:

Calling "g_main_context_pending" increases reference count for the ready sources, in the internal call to g_main_context_check. This function calls internal "g_main_context_iterate", with "dispatch" parameter set to "FALSE". Normally, whenever g_main_context_iterate is called with "dispatch" parameter set to "TRUE", internal call of "g_main_context_dispatch" decreases the reference count of the dispatched sources.

Steps to reproduce:

   1.  4 sources are attached to the newly created context, two of which with priority "-1" and two others with "1" and "2".
   2. First two sources will be marked as "READY" after internal call of "g_main_context_check" and reference count for them will be increased. 

If "g_main_context_pending" would not have been called here, the following warnings would appear after the invocation of "g_main_context_unref" in the "for":

      (process:7320): GLib-WARNING **: gmain.c:1417: ref_count == 0, 
                           but source is still attached to a context!
      (process:7320): GLib-WARNING **: gmain.c:1417: ref_count == 0, 
                           but source is still attached to a context!

The absence of these warnings upon invocation of the sample shows that reference count for ready sources was increased during the call to "g_main_context_pending".

The detailed bug description can be found at:
Comment 1 Areg Beketovski 2007-09-24 12:17:13 UTC
Created attachment 96112 [details]
The test case reproducing the bug
Comment 2 Namhyung Kim 2016-08-16 14:19:24 UTC
Created attachment 333422 [details] [review]
proposed fix

I've also identified this bug during code reading.  What about adding g_main_context_check_ref() to control refcounting and making g_main_context_check() calls it (with TRUE)?
Comment 3 Hyungwon Hwang 2016-08-22 11:15:06 UTC
Hi, Areg & Namhyung.

@ Areg
As I read the API, Areg need to use g_source_destroy() instead of calling g_source_unref() directly.

@ Namhyung
It seems sane to increase the ref count of the ready sources which are inserted to context->pending_dispatches, by calling g_main_context_pending(). The ref count of that sources will be un-ref when the sources are dispatched or the context is freed.
Comment 4 Namhyung Kim 2016-08-23 01:52:23 UTC
Hello Hyungwon,

Thanks for the review.  I was not sure how this API is used.  It seems g_main_context_pending() followed by g_main_context_dispatch() would be fine, but if not, there will be a leak.  I think it's better protecting the resource from the API misuse or adding some comment about this at least.
Comment 5 GNOME Infrastructure Team 2018-05-24 11:06:49 UTC
-- GitLab Migration Automatic Message --

This bug has been migrated to GNOME's GitLab instance and has been closed from further activity.

You can subscribe and participate further through the new bug through this link to our GitLab instance: