简体   繁体   English

Valgrind报告使用glib数据类型时内存“可能已丢失”

[英]Valgrind reports memory 'possibly lost' when using glib data types

I'm developing a library using a number of glib datastructures (GHashTable, GSList etc.). 我正在使用许多glib数据结构(GHashTable,GSList等)开发库。 I've been checking my code frequently for memory leaks using valgrind. 我一直在使用valgrind经常检查我的代码是否有内存泄漏。 Most of the issues valgrind points out are quite easy to fix, however there's a few that I can't figure out. valgrind指出的大多数问题很容易解决,但有一些我无法弄清楚。

All of these are reported as 'possibly lost'. 所有这些都被报道为“可能丢失”。

At the top of the valgrind stacktrace, I always find the same 4 libraries: 在valgrind stacktrace的顶部,我总能找到相同的4个库:

==29997== 1,512 bytes in 3 blocks are possibly lost in loss record 24 of 25
==29997==    at 0x4004B11: memalign (vg_replace_malloc.c:532)
==29997==    by 0x4004B6B: posix_memalign (vg_replace_malloc.c:660)
==29997==    by 0x5E9AC4: ??? (in /lib/libglib-2.0.so.0.1200.3)
==29997==    by 0x5EA4FE: g_slice_alloc (in /lib/libglib-2.0.so.0.1200.3)

Further down in the call stack, there is always a call to a glib function, such as g_key_file_new(), g_slist_prepend(), g_strsplit(), g_key_file_load_from_file(), g_file_get_contents(). 在调用堆栈中,总是调用glib函数,例如g_key_file_new(),g_slist_prepend(),g_strsplit(),g_key_file_load_from_file(),g_file_get_contents()。

My questions are: 我的问题是:

  • Has anyone come across this and found a way around it? 有没有人遇到这个并找到了解决方法?

  • Or is this something I can disregard? 或者这是我可以忽略的东西? Is it due to glib using memory pools, as suggested here ? 是否由于glib使用内存池,如此处所示

I am using 我在用

  • valgrind-3.5.0 的valgrind-3.5.0
  • glib-2.12.3 巧舌如簧-2.12.3
  • gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-48) gcc(GCC)4.1.2 20080704(Red Hat 4.1.2-48)
  • CentOS release 5.5 (Final) CentOS 5.5版(最终版)

GLib has a few features that confuse Valgrind. GLib有一些让Valgrind迷惑的功能。

One is memory pools (g_slice in newer glib, "mem chunks" in older). 一个是内存池(较新的glib中的g_slice,较旧的“mem chunks”)。 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 您可以使用它来禁用切片分配器: 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. 第二个问题是,有时GLib会避免初始化新内存或在释放的切片/块中保留死指针。 You can fix this with: G_DEBUG=gc-friendly valgrind myprogram 您可以使用以下方法解决此问题: G_DEBUG=gc-friendly valgrind myprogram

So together of course: G_DEBUG=gc-friendly G_SLICE=always-malloc valgrind myprogram 所以当然在一起: 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. 第三个问题是GLib具有全局变量,这些变量根本不会被释放,但被认为是永久性程序状态。 For example registered GType are never unloaded, and a few others. 例如,注册的GType永远不会被卸载,还有一些其他的。 This is not fixable, but valgrind should show these global allocations as reachable, rather than as lost. 这是不可修复的,但是valgrind应该将这些全局分配显示为可达,而不是丢失。

glib-2.12 is quite old. glib-2.12很老了。

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. 尝试获取glib-2.24,编译并安装它(例如--prefix = / usr / local / glib-2.24 )然后用它来编译你的应用程序。

If you still have this, try to read the glib manual again :) 如果你仍然有这个,请尝试再次阅读glib手册:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM