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

后端 未结 2 867
日久生厌
日久生厌 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.

提交回复
热议问题