annoying bug in glib (about eating CPU)



Hi,

Here the complete description of the problem
and how to fix it (I think). Thanks.
------------------------------------------------------------------------------------------------------------------
Context: an image viewer in idle time

Bug: when starting, the viewer takes less than 1% CPU in idle time
     (which is pretty normal), but after some actions (like loading some
     images from a renderer via a socket mechanism), the idle time
     increases up to 2 to 20% CPU (depending of how long takes the
     image loading)

Platform: Linux 2.6.37.6 x64_64

the image viewier uses wxWidgets version 2.8.12,
based on gdk/gtk and glib

libraries gdk/gtk used: version 2.24.4
library   glib    used: version 2.28.6

this bug happens for other Linux version (like 2.6.28 and 3.2.13)
and glib version (like 2.14.6 and 2.24.1)
------------------------------------------------------------------------
What's going on? Here some traces which explains the bug

step 1: g_main_dispatch is called

g_main_dispatch before loop - context->pending_dispatches->len: 1
* context: 0xc58c50, source: 0xc59b40, ref_count: 2
before dispatch - context: 0xc58c50, source: 0xc59b40, ref_count: 2

step 2: resulting in some calls from wxWidgets (wxApp_idle_callback)
        (when wxTheApp->ProcessIdle() returns false)

g_idle_add_full - source: 0x7fa50c000d60, ref_count: 1
g_source_remove - source: 0x7fa50c000d60, ref_count: 0
g_idle_add_full - source: 0x7fa50c000d60, ref_count: 1
g_source_remove - source: 0x7fa50c000d60, ref_count: 0
g_idle_add_full - source: 0x12fca10, ref_count: 1
g_source_remove - source: 0x12fca10, ref_count: 0
g_idle_add_full - source: 0x1080da0, ref_count: 1
g_source_remove - source: 0x1080da0, ref_count: 0
g_idle_add_full - source: 0xcee0a0, ref_count: 1
g_source_remove - source: 0xcee0a0, ref_count: 0
g_idle_add_full - source: 0x11bee30, ref_count: 1
g_source_remove - source: 0x11bee30, ref_count: 0
g_idle_add_full - source: 0x11bee10, ref_count: 1

step 3: wxApp_idle_callback calls gtk_events_pending()
        (when wxTheApp->ProcessIdle() returns true)

        this result in a call to g_main_context_check (via
        g_main_context_iterate, via g_main_context_pending)
        which adds some sources to context->pending_dispatches
        (depending of some priority)

g_main_context_check - context->pending_dispatches->len: 1
* context: 0xc58c50, source: 0x11bee10, ref_count: 2

        notice that ref_count of source "0x11bee10" is now 2,
        which is correct; but what is wrong is the array
        context->pending_dispatches has been modified while
        g_main_dispatch is currently processed source "0xc59b40"

step 4: g_main_dispatch terminates (setting the length of
        context->pending_dispatches to 0)

step 5: now gtk_events_pending() is called again (but now before
        g_main_dispatch, from wxWidgets inside wxEventLoopManual::Run)

        this result again in a call to g_main_context_check (via
        g_main_context_iterate, via g_main_context_pending)
        which adds some sources to context->pending_dispatches
        (depending of some priority)

g_main_context_check - context->pending_dispatches->len: 1
* context: 0xc58c50, source: 0x11bee10, ref_count: 3

        notice that ref_count of source "0x11bee10" is now 3,
        which is bad; normally, g_main_context_prepare unref
        sources in array context->pending_dispatches, but because
        g_main_dispatch has set 0 for this array (see step 4),
        all new sources in this array (see step 3) are ignored,
        see next two steps for the bug

step 6: g_main_dispatch is called again

g_main_dispatch before loop - context->pending_dispatches->len: 1
* context: 0xc58c50, source: 0x11bee10, ref_count: 3
dispatch - context: 0xc58c50, source: 0x11bee10, ref_count: 3

step 7: resulting in some calls from wxWidgets (wxApp_idle_callback)
        (when wxTheApp->ProcessIdle() returns false)

g_source_remove - source: 0x11bee10, ref_count: 2

        now the bug appears, the source "0x11bee10" will not be
        destroy and sticks in the list context->source_list
---------------------------------------------------------------------------
I put some prints at the end of function g_main_context_iterate
to illustrate this particular bug (the trace indicates the number
of microsecs takes by next_valid_source after 1000 calls, it also
indicates the number of sources scanned by this function in its
while loop; finally, we print the list context->source_list)

before loading images by the viewer:

time in next_valid_source after 1000 tics: 602 micros, nSScan: 5934
* context: 0xc58c50, source: 0xc58b60, ref_count: 7
* context: 0xc58c50, source: 0xe9a300, ref_count: 1
* context: 0xc58c50, source: 0xc59b40, ref_count: 2

after loading images by the viewer (notice that ref_count of main
source "0xc58b60" stills going up...):

time in next_valid_source after 1000 tics: 12040 micros, nSScan: 632000
* context: 0xc58c50, source: 0xc58b60, ref_count: 435
* context: 0xc58c50, source: 0xe9a300, ref_count: 1
* context: 0xc58c50, source: 0x11bee10, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c000d60, ref_count: 1
* context: 0xc58c50, source: 0x1391130, ref_count: 1
* context: 0xc58c50, source: 0xfe2cd0, ref_count: 1
* context: 0xc58c50, source: 0x111deb0, ref_count: 1
* context: 0xc58c50, source: 0x11beea0, ref_count: 1
* context: 0xc58c50, source: 0x13972f0, ref_count: 1
* context: 0xc58c50, source: 0x13921d0, ref_count: 1
* context: 0xc58c50, source: 0x12fcc30, ref_count: 1
* context: 0xc58c50, source: 0x13a0360, ref_count: 1
* context: 0xc58c50, source: 0x1393820, ref_count: 1
* context: 0xc58c50, source: 0x13a2e60, ref_count: 1
* context: 0xc58c50, source: 0x13a4540, ref_count: 1
* context: 0xc58c50, source: 0x1319350, ref_count: 1
* context: 0xc58c50, source: 0x12fdda0, ref_count: 1
* context: 0xc58c50, source: 0x13a1110, ref_count: 1
* context: 0xc58c50, source: 0x12ffbd0, ref_count: 1
* context: 0xc58c50, source: 0x1302c20, ref_count: 1
* context: 0xc58c50, source: 0x13065a0, ref_count: 1
* context: 0xc58c50, source: 0x1300220, ref_count: 1
* context: 0xc58c50, source: 0x13019f0, ref_count: 1
* context: 0xc58c50, source: 0x13940d0, ref_count: 1
* context: 0xc58c50, source: 0x130fef0, ref_count: 1
* context: 0xc58c50, source: 0x1312020, ref_count: 1
* context: 0xc58c50, source: 0x1309a60, ref_count: 1
* context: 0xc58c50, source: 0x1309300, ref_count: 1
* context: 0xc58c50, source: 0x12ff1d0, ref_count: 1
* context: 0xc58c50, source: 0x13200e0, ref_count: 1
* context: 0xc58c50, source: 0x131f580, ref_count: 1
* context: 0xc58c50, source: 0x1777dd0, ref_count: 1
* context: 0xc58c50, source: 0x130e340, ref_count: 1
* context: 0xc58c50, source: 0x1778820, ref_count: 1
* context: 0xc58c50, source: 0x17786a0, ref_count: 1
* context: 0xc58c50, source: 0x1323180, ref_count: 1
* context: 0xc58c50, source: 0x13372c0, ref_count: 1
* context: 0xc58c50, source: 0x131f460, ref_count: 1
* context: 0xc58c50, source: 0x11bb700, ref_count: 1
* context: 0xc58c50, source: 0x1335c30, ref_count: 1
* context: 0xc58c50, source: 0x17848b0, ref_count: 1
* context: 0xc58c50, source: 0x177fbb0, ref_count: 1
* context: 0xc58c50, source: 0x133e020, ref_count: 1
* context: 0xc58c50, source: 0x1307b20, ref_count: 1
* context: 0xc58c50, source: 0x178aa80, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c6c6a30, ref_count: 1
* context: 0xc58c50, source: 0x1776970, ref_count: 1
* context: 0xc58c50, source: 0x178ad60, ref_count: 1
* context: 0xc58c50, source: 0x1789370, ref_count: 1
* context: 0xc58c50, source: 0x1788bf0, ref_count: 1
* context: 0xc58c50, source: 0x17926d0, ref_count: 1
* context: 0xc58c50, source: 0x132acf0, ref_count: 1
* context: 0xc58c50, source: 0x1372d50, ref_count: 1
* context: 0xc58c50, source: 0x1374ea0, ref_count: 1
* context: 0xc58c50, source: 0x133feb0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c0a0fc0, ref_count: 1
* context: 0xc58c50, source: 0x132d1c0, ref_count: 1
* context: 0xc58c50, source: 0x1330aa0, ref_count: 1
* context: 0xc58c50, source: 0x132fed0, ref_count: 1
* context: 0xc58c50, source: 0x1328f00, ref_count: 1
* context: 0xc58c50, source: 0x13327f0, ref_count: 1
* context: 0xc58c50, source: 0x132fba0, ref_count: 1
* context: 0xc58c50, source: 0x1378220, ref_count: 1
* context: 0xc58c50, source: 0x1364f20, ref_count: 1
* context: 0xc58c50, source: 0x136b790, ref_count: 1
* context: 0xc58c50, source: 0x136c670, ref_count: 1
* context: 0xc58c50, source: 0x136fef0, ref_count: 1
* context: 0xc58c50, source: 0x1332470, ref_count: 1
* context: 0xc58c50, source: 0x1348120, ref_count: 1
* context: 0xc58c50, source: 0x134f620, ref_count: 1
* context: 0xc58c50, source: 0x1350250, ref_count: 1
* context: 0xc58c50, source: 0x135cdd0, ref_count: 1
* context: 0xc58c50, source: 0x135a180, ref_count: 1
* context: 0xc58c50, source: 0x1359ef0, ref_count: 1
* context: 0xc58c50, source: 0x176e170, ref_count: 1
* context: 0xc58c50, source: 0x135efe0, ref_count: 1
* context: 0xc58c50, source: 0x135f050, ref_count: 1
* context: 0xc58c50, source: 0x176e830, ref_count: 1
* context: 0xc58c50, source: 0x176b4b0, ref_count: 1
* context: 0xc58c50, source: 0x135e2c0, ref_count: 1
* context: 0xc58c50, source: 0x175d5b0, ref_count: 1
* context: 0xc58c50, source: 0x17735f0, ref_count: 1
* context: 0xc58c50, source: 0x1768920, ref_count: 1
* context: 0xc58c50, source: 0x1772d70, ref_count: 1
* context: 0xc58c50, source: 0x17601a0, ref_count: 1
* context: 0xc58c50, source: 0x1766580, ref_count: 1
* context: 0xc58c50, source: 0x17388e0, ref_count: 1
* context: 0xc58c50, source: 0x1765d40, ref_count: 1
* context: 0xc58c50, source: 0x1761dd0, ref_count: 1
* context: 0xc58c50, source: 0x1761b50, ref_count: 1
* context: 0xc58c50, source: 0x173b3d0, ref_count: 1
* context: 0xc58c50, source: 0x1740650, ref_count: 1
* context: 0xc58c50, source: 0x1760910, ref_count: 1
* context: 0xc58c50, source: 0x17492c0, ref_count: 1
* context: 0xc58c50, source: 0x1742c60, ref_count: 1
* context: 0xc58c50, source: 0x173f190, ref_count: 1
* context: 0xc58c50, source: 0x173e2a0, ref_count: 1
* context: 0xc58c50, source: 0x1741d30, ref_count: 1
* context: 0xc58c50, source: 0x1763cc0, ref_count: 1
* context: 0xc58c50, source: 0x174c5c0, ref_count: 1
* context: 0xc58c50, source: 0x1756980, ref_count: 1
* context: 0xc58c50, source: 0x1759670, ref_count: 1
* context: 0xc58c50, source: 0x1747530, ref_count: 1
* context: 0xc58c50, source: 0x1740d20, ref_count: 1
* context: 0xc58c50, source: 0x1755c40, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c6e0c70, ref_count: 1
* context: 0xc58c50, source: 0x17209b0, ref_count: 1
* context: 0xc58c50, source: 0x1728290, ref_count: 1
* context: 0xc58c50, source: 0x1723730, ref_count: 1
* context: 0xc58c50, source: 0x1727530, ref_count: 1
* context: 0xc58c50, source: 0x1725770, ref_count: 1
* context: 0xc58c50, source: 0x17252f0, ref_count: 1
* context: 0xc58c50, source: 0x1728450, ref_count: 1
* context: 0xc58c50, source: 0x172bab0, ref_count: 1
* context: 0xc58c50, source: 0x1736d80, ref_count: 1
* context: 0xc58c50, source: 0x17251e0, ref_count: 1
* context: 0xc58c50, source: 0x1737630, ref_count: 1
* context: 0xc58c50, source: 0x16f9490, ref_count: 1
* context: 0xc58c50, source: 0x16fdb60, ref_count: 1
* context: 0xc58c50, source: 0x16ff2c0, ref_count: 1
* context: 0xc58c50, source: 0x1707430, ref_count: 1
* context: 0xc58c50, source: 0x17081a0, ref_count: 1
* context: 0xc58c50, source: 0x170ef90, ref_count: 1
* context: 0xc58c50, source: 0x1704bb0, ref_count: 1
* context: 0xc58c50, source: 0x17049b0, ref_count: 1
* context: 0xc58c50, source: 0x16f75f0, ref_count: 1
* context: 0xc58c50, source: 0x1708f00, ref_count: 1
* context: 0xc58c50, source: 0x1762930, ref_count: 1
* context: 0xc58c50, source: 0x1708ad0, ref_count: 1
* context: 0xc58c50, source: 0x1716fe0, ref_count: 1
* context: 0xc58c50, source: 0x171c4a0, ref_count: 1
* context: 0xc58c50, source: 0x1717da0, ref_count: 1
* context: 0xc58c50, source: 0x76426b0, ref_count: 1
* context: 0xc58c50, source: 0x170ccd0, ref_count: 1
* context: 0xc58c50, source: 0x170be50, ref_count: 1
* context: 0xc58c50, source: 0x1713b40, ref_count: 1
* context: 0xc58c50, source: 0x7643be0, ref_count: 1
* context: 0xc58c50, source: 0x171bf60, ref_count: 1
* context: 0xc58c50, source: 0x768c360, ref_count: 1
* context: 0xc58c50, source: 0x764ddb0, ref_count: 1
* context: 0xc58c50, source: 0x17189c0, ref_count: 1
* context: 0xc58c50, source: 0x171a620, ref_count: 1
* context: 0xc58c50, source: 0x764c1c0, ref_count: 1
* context: 0xc58c50, source: 0x76475f0, ref_count: 1
* context: 0xc58c50, source: 0x76d2240, ref_count: 1
* context: 0xc58c50, source: 0x76d49b0, ref_count: 1
* context: 0xc58c50, source: 0x7652650, ref_count: 1
* context: 0xc58c50, source: 0x76553c0, ref_count: 1
* context: 0xc58c50, source: 0x76d96a0, ref_count: 1
* context: 0xc58c50, source: 0x76d7a20, ref_count: 1
* context: 0xc58c50, source: 0x7658be0, ref_count: 1
* context: 0xc58c50, source: 0x7658f60, ref_count: 1
* context: 0xc58c50, source: 0x76d11e0, ref_count: 1
* context: 0xc58c50, source: 0x7662af0, ref_count: 1
* context: 0xc58c50, source: 0x7664d00, ref_count: 1
* context: 0xc58c50, source: 0x765b430, ref_count: 1
* context: 0xc58c50, source: 0x7667530, ref_count: 1
* context: 0xc58c50, source: 0x7662990, ref_count: 1
* context: 0xc58c50, source: 0xea6720, ref_count: 1
* context: 0xc58c50, source: 0x7668e40, ref_count: 1
* context: 0xc58c50, source: 0x76655b0, ref_count: 1
* context: 0xc58c50, source: 0x7684860, ref_count: 1
* context: 0xc58c50, source: 0x76883c0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c854810, ref_count: 1
* context: 0xc58c50, source: 0x76854e0, ref_count: 1
* context: 0xc58c50, source: 0x7675300, ref_count: 1
* context: 0xc58c50, source: 0x76759d0, ref_count: 1
* context: 0xc58c50, source: 0x768bad0, ref_count: 1
* context: 0xc58c50, source: 0x767ae80, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c8578b0, ref_count: 1
* context: 0xc58c50, source: 0x7675570, ref_count: 1
* context: 0xc58c50, source: 0x76adee0, ref_count: 1
* context: 0xc58c50, source: 0x7687370, ref_count: 1
* context: 0xc58c50, source: 0x76b1590, ref_count: 1
* context: 0xc58c50, source: 0x76b4da0, ref_count: 1
* context: 0xc58c50, source: 0x76b1220, ref_count: 1
* context: 0xc58c50, source: 0x76b2fb0, ref_count: 1
* context: 0xc58c50, source: 0x767a110, ref_count: 1
* context: 0xc58c50, source: 0x76b7520, ref_count: 1
* context: 0xc58c50, source: 0x76bd520, ref_count: 1
* context: 0xc58c50, source: 0x76bfb80, ref_count: 1
* context: 0xc58c50, source: 0x76bd910, ref_count: 1
* context: 0xc58c50, source: 0x76beb10, ref_count: 1
* context: 0xc58c50, source: 0x76be050, ref_count: 1
* context: 0xc58c50, source: 0x76bf5b0, ref_count: 1
* context: 0xc58c50, source: 0x76901e0, ref_count: 1
* context: 0xc58c50, source: 0x7691080, ref_count: 1
* context: 0xc58c50, source: 0x76cbbf0, ref_count: 1
* context: 0xc58c50, source: 0x76c5ea0, ref_count: 1
* context: 0xc58c50, source: 0x76948d0, ref_count: 1
* context: 0xc58c50, source: 0x769a740, ref_count: 1
* context: 0xc58c50, source: 0x7693a90, ref_count: 1
* context: 0xc58c50, source: 0x769a270, ref_count: 1
* context: 0xc58c50, source: 0x769dfd0, ref_count: 1
* context: 0xc58c50, source: 0x76a10c0, ref_count: 1
* context: 0xc58c50, source: 0x7698710, ref_count: 1
* context: 0xc58c50, source: 0x76a0cd0, ref_count: 1
* context: 0xc58c50, source: 0x76a5eb0, ref_count: 1
* context: 0xc58c50, source: 0x76a77f0, ref_count: 1
* context: 0xc58c50, source: 0x76a5680, ref_count: 1
* context: 0xc58c50, source: 0x76a5330, ref_count: 1
* context: 0xc58c50, source: 0x769a8a0, ref_count: 1
* context: 0xc58c50, source: 0x7694450, ref_count: 1
* context: 0xc58c50, source: 0x76a7760, ref_count: 1
* context: 0xc58c50, source: 0x76a22f0, ref_count: 1
* context: 0xc58c50, source: 0x76ab000, ref_count: 1
* context: 0xc58c50, source: 0x14483120, ref_count: 1
* context: 0xc58c50, source: 0x76ac810, ref_count: 1
* context: 0xc58c50, source: 0x144a6c10, ref_count: 1
* context: 0xc58c50, source: 0x144a7360, ref_count: 1
* context: 0xc58c50, source: 0x14480220, ref_count: 1
* context: 0xc58c50, source: 0x144ad110, ref_count: 1
* context: 0xc58c50, source: 0x144a9e30, ref_count: 1
* context: 0xc58c50, source: 0x144b3df0, ref_count: 1
* context: 0xc58c50, source: 0x144b4700, ref_count: 1
* context: 0xc58c50, source: 0x144aefc0, ref_count: 1
* context: 0xc58c50, source: 0x144b8240, ref_count: 1
* context: 0xc58c50, source: 0x144b2f40, ref_count: 1
* context: 0xc58c50, source: 0x144b5440, ref_count: 1
* context: 0xc58c50, source: 0x14486690, ref_count: 1
* context: 0xc58c50, source: 0x144873c0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c82e820, ref_count: 1
* context: 0xc58c50, source: 0x1448eee0, ref_count: 1
* context: 0xc58c50, source: 0x1448a6e0, ref_count: 1
* context: 0xc58c50, source: 0x14489850, ref_count: 1
* context: 0xc58c50, source: 0x1449b480, ref_count: 1
* context: 0xc58c50, source: 0x14496fc0, ref_count: 1
* context: 0xc58c50, source: 0x144887d0, ref_count: 1
* context: 0xc58c50, source: 0x14491010, ref_count: 1
* context: 0xc58c50, source: 0x1443fbf0, ref_count: 1
* context: 0xc58c50, source: 0x144206a0, ref_count: 1
* context: 0xc58c50, source: 0x14422190, ref_count: 1
* context: 0xc58c50, source: 0x14424040, ref_count: 1
* context: 0xc58c50, source: 0x144a2900, ref_count: 1
* context: 0xc58c50, source: 0x14493220, ref_count: 1
* context: 0xc58c50, source: 0x14420a80, ref_count: 1
* context: 0xc58c50, source: 0x14471410, ref_count: 1
* context: 0xc58c50, source: 0x144294a0, ref_count: 1
* context: 0xc58c50, source: 0x144271d0, ref_count: 1
* context: 0xc58c50, source: 0x144a00d0, ref_count: 1
* context: 0xc58c50, source: 0x14470350, ref_count: 1
* context: 0xc58c50, source: 0x1447b700, ref_count: 1
* context: 0xc58c50, source: 0x1442bba0, ref_count: 1
* context: 0xc58c50, source: 0x1447ba00, ref_count: 1
* context: 0xc58c50, source: 0x1447c800, ref_count: 1
* context: 0xc58c50, source: 0x1442f110, ref_count: 1
* context: 0xc58c50, source: 0x144b8e70, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c834900, ref_count: 1
* context: 0xc58c50, source: 0x144335c0, ref_count: 1
* context: 0xc58c50, source: 0x144302b0, ref_count: 1
* context: 0xc58c50, source: 0x144369d0, ref_count: 1
* context: 0xc58c50, source: 0x1443a450, ref_count: 1
* context: 0xc58c50, source: 0x14477c60, ref_count: 1
* context: 0xc58c50, source: 0x1443c4a0, ref_count: 1
* context: 0xc58c50, source: 0x1443b2b0, ref_count: 1
* context: 0xc58c50, source: 0x14446260, ref_count: 1
* context: 0xc58c50, source: 0x14447ad0, ref_count: 1
* context: 0xc58c50, source: 0x1446b700, ref_count: 1
* context: 0xc58c50, source: 0x144488a0, ref_count: 1
* context: 0xc58c50, source: 0x14447190, ref_count: 1
* context: 0xc58c50, source: 0x1444c0a0, ref_count: 1
* context: 0xc58c50, source: 0x1444dd00, ref_count: 1
* context: 0xc58c50, source: 0x1446f930, ref_count: 1
* context: 0xc58c50, source: 0x14454a90, ref_count: 1
* context: 0xc58c50, source: 0x1444c330, ref_count: 1
* context: 0xc58c50, source: 0x1445f6b0, ref_count: 1
* context: 0xc58c50, source: 0x1445b630, ref_count: 1
* context: 0xc58c50, source: 0x1445f630, ref_count: 1
* context: 0xc58c50, source: 0x1445db30, ref_count: 1
* context: 0xc58c50, source: 0x14468560, ref_count: 1
* context: 0xc58c50, source: 0x144653b0, ref_count: 1
* context: 0xc58c50, source: 0x144616d0, ref_count: 1
* context: 0xc58c50, source: 0x20eefc10, ref_count: 1
* context: 0xc58c50, source: 0x20ef1a20, ref_count: 1
* context: 0xc58c50, source: 0x20eebd10, ref_count: 1
* context: 0xc58c50, source: 0x20efa1a0, ref_count: 1
* context: 0xc58c50, source: 0x144663b0, ref_count: 1
* context: 0xc58c50, source: 0x20eff410, ref_count: 1
* context: 0xc58c50, source: 0x20f00180, ref_count: 1
* context: 0xc58c50, source: 0xc59b40, ref_count: 2
* context: 0xc58c50, source: 0x7fa50c001dc0, ref_count: 1
* context: 0xc58c50, source: 0x1319900, ref_count: 1
* context: 0xc58c50, source: 0x7fa51409ff40, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c4534a0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c3b3770, ref_count: 1
* context: 0xc58c50, source: 0x7fa5140a5fe0, ref_count: 1
* context: 0xc58c50, source: 0x1787280, ref_count: 1
* context: 0xc58c50, source: 0x137ccc0, ref_count: 1
* context: 0xc58c50, source: 0x134c650, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c6d15d0, ref_count: 1
* context: 0xc58c50, source: 0x13613e0, ref_count: 1
* context: 0xc58c50, source: 0x1769ec0, ref_count: 1
* context: 0xc58c50, source: 0x173a0d0, ref_count: 1
* context: 0xc58c50, source: 0x174b910, ref_count: 1
* context: 0xc58c50, source: 0x174f450, ref_count: 1
* context: 0xc58c50, source: 0x1732880, ref_count: 1
* context: 0xc58c50, source: 0x764b2d0, ref_count: 1
* context: 0xc58c50, source: 0x76d9390, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c8468e0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c84a240, ref_count: 1
* context: 0xc58c50, source: 0x765a810, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c84c810, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c856430, ref_count: 1
* context: 0xc58c50, source: 0x76ccbe0, ref_count: 1
* context: 0xc58c50, source: 0x76c20c0, ref_count: 1
* context: 0xc58c50, source: 0x76c0d90, ref_count: 1
* context: 0xc58c50, source: 0x7fa50c822580, ref_count: 1
* context: 0xc58c50, source: 0x144b5780, ref_count: 1
* context: 0xc58c50, source: 0x144b92c0, ref_count: 1
* context: 0xc58c50, source: 0x144a07d0, ref_count: 1
* context: 0xc58c50, source: 0x7fa51459b400, ref_count: 1
* context: 0xc58c50, source: 0x14453bc0, ref_count: 1
* context: 0xc58c50, source: 0x14466210, ref_count: 1
* context: 0xc58c50, source: 0x20ef8980, ref_count: 1
* context: 0xc58c50, source: 0x20f002a0, ref_count: 1
* context: 0xc58c50, source: 0x7fa50d5c86b0, ref_count: 1
---------------------------------------------------------------------------
How to fix the bug?

I think the simple solution is that (but since I'm not
an expert of glib, maybe I don't see all the implications
and complications):

- fisrt, in g_main_context_prepare, I disable the loop:

  /* If recursing, clear list of pending dispatches */

  for (i = 0; i < context->pending_dispatches->len; i++)
    {
      if (context->pending_dispatches->pdata[i])
SOURCE_UNREF ((GSource *)context->pending_dispatches->pdata[i], context);
    }
  g_ptr_array_set_size (context->pending_dispatches, 0);

- second, in g_main_dispatch, I replace the for loop by a while loop
  like this:

  while (context->pending_dispatches->len > 0)
    {
      GSource *source = context->pending_dispatches->pdata[0];

      context->pending_dispatches->pdata[i] = NULL;
      g_ptr_array_remove (context->pending_dispatches, source);

      etc.

- in other words, I let the function g_main_context_check adds
  source in array context->pending_dispatches, but only function
  g_main_dispatch is responsible to remove source from it
-------------------------------------------------------------------------------
Here the same prints after the fix:

before loading images by the viewer:

time in next_valid_source after 1000 tics: 588 micros, nSScan: 4040
* context: 0xc58c50, source: 0xc58b60, ref_count: 2
* context: 0xc58c50, source: 0xc59b40, ref_count: 2

after loading images by the viewer:

time in next_valid_source after 1000 tics: 556 micros, nSScan: 4000
* context: 0xc58c50, source: 0xc58b60, ref_count: 2
* context: 0xc58c50, source: 0xc59b40, ref_count: 2
-------------------------------------------------------------------------------
Thanks for reading this.





[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]