本文介绍了Valgrind的报告内存使用巧舌如簧数据类型时“可能失去”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发使用了一些油嘴滑舌的数据结构(GHashTable,GSList等)的库。我一直在使用的valgrind检查我的code频繁内存泄漏。大多数问题的valgrind指出是很容易解决,但是有几个是我想不通。

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 the issues valgrind points out are quite easy to fix, however there's a few that I can't figure out.

所有这些都报告为可能失去。

All of these are reported as 'possibly lost'.

在Valgrind的堆栈跟踪的顶部,我总是发现相同的4个库:

At the top of the valgrind stacktrace, I always find the same 4 libraries:

==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)

在调用栈中再往下,总有一个油嘴函数的调用,如g_key_file_new(),g_slist_ prePEND(),g_strsplit(),g_key_file_load_from_file(),g_file_get_contents()。

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().

我的问题是:


  • 有没有人碰到,发现这个办法解决它?

  • 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?

我使用


  • 的valgrind-3.5.0

  • 巧舌如簧-2.12.3

  • 海湾合作委员会(GCC)4.1.2 20080704(红帽4.1.2-48)

  • CentOS版本5.5(决赛)

推荐答案

GLib的有混淆Valgrind的一些功能。

GLib has a few features that confuse Valgrind.

一个是内存池(G_SLICE在新巧舌如簧,MEM块中老年)。这些是用于小型物体,如列表中的节点专用分配器。您可以使用该禁用片分配器:
= G_SLICE永远的malloc的valgrind myprogram

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

第二个问题是,有时GLib的将避免初始化新的内存或守尸指针中解放出来的片/块。你可以解决这个问题:
G_DEBUG = GC友好的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

所以说起来,当然:
G_DEBUG = GC友好G_SLICE =永远的malloc的valgrind myprogram

第三个问题是,GLib的具有只是永远不会被释放,但考虑长期方案的状态全局变量。例如注册的GType永远不会卸载,和其他几个人。这是不可固定,但Valgrind的应该显示这些全球分配作为可达的,而不是作为丢失。

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.

这篇关于Valgrind的报告内存使用巧舌如簧数据类型时“可能失去”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 23:45