Valgrind reports memory 'possibly lost' when using glib data types

后端 未结 2 866
日久生厌
日久生厌 2020-12-08 10:31

I\'m developing a library using a number of glib datastructures (GHashTable, GSList etc.). I\'ve been checking my code frequently for memory leaks using valgrind. Most of th

相关标签:
2条回答
  • 2020-12-08 11:12

    GLib has a few features that confuse Valgrind.

    One is memory pools (g_slice in newer glib, "mem chunks" in older). These are specialized allocators used for small objects such as list nodes. You can use this to disable the slice allocator: G_SLICE=always-malloc valgrind myprogram

    A second issue is that sometimes GLib would avoid initializing new memory or keep dead pointers in freed slices/chunks. You can fix this with: G_DEBUG=gc-friendly valgrind myprogram

    So together of course: G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind myprogram

    A third issue is that GLib has global variables that are simply never freed but considered permanent program state. For example registered GType are never unloaded, and a few others. This is not fixable, but valgrind should show these global allocations as reachable, rather than as lost.

    0 讨论(0)
  • 2020-12-08 11:14

    glib-2.12 is quite old.

    Try getting glib-2.24, compile and install it (with --prefix=/usr/local/glib-2.24 for example) then use it to compile your application.

    If you still have this, try to read the glib manual again :)

    0 讨论(0)
提交回复
热议问题