Valgrind使用空的main显示内存泄漏,不包含头文件 [英] Valgrind shows memory leak with empty main without including headers

查看:94
本文介绍了Valgrind使用空的main显示内存泄漏,不包含头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目存在内存泄漏.我可以找到它们,所以我清理了main.cpp文件,现在看起来像这样:

I have project with memory leaks. I can find them, so I cleaned up my main.cpp file, and now it looks like this:

int main()
{
   return 0;
}

当我使用以下命令检查内存时:

When I check memory with the command:

valgrind --leak-check=full --show-leak-kinds=all ./MyProgram > log1.txt 2>&1

我遇到了这些错误:

==5219== 
==5219== LEAK SUMMARY:
==5219==    definitely lost: 0 bytes in 0 blocks
==5219==    indirectly lost: 0 bytes in 0 blocks
==5219==      possibly lost: 728 bytes in 18 blocks
==5219==    still reachable: 44,676 bytes in 224 blocks
==5219==                       of which reachable via heuristic:
==5219==                         newarray           : 832 bytes in 16 blocks
==5219==         suppressed: 0 bytes in 0 blocks
==5219== 
==5219== For counts of detected and suppressed errors, rerun with: -v
==5219== ERROR SUMMARY: 18 errors from 18 contexts (suppressed: 0 from 0)

一些错误是:

==5219== 2,048 bytes in 1 blocks are still reachable in loss record 240 of 242
==5219==    at 0x402E2EC: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5219==    by 0x5F7C151: g_realloc (in /lib/i386-linux-gnu/libglib-2.0.so.0.4600.2)
==5219==    by 0x5F06BCB: g_value_register_transform_func (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x5F08D6A: ??? (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x5ED9A2D: ??? (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x400EDCF: call_init.part.0 (dl-init.c:72)
==5219==    by 0x400EEDF: call_init (dl-init.c:30)
==5219==    by 0x400EEDF: _dl_init (dl-init.c:120)
==5219==    by 0x4000ACE: ??? (in /lib/i386-linux-gnu/ld-2.21.so)

==5219== 8 bytes in 1 blocks are possibly lost in loss record 95 of 242
==5219==    at 0x402E0D8: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==5219==    by 0x5F7C0DA: g_malloc0 (in /lib/i386-linux-gnu/libglib-2.0.so.0.4600.2)
==5219==    by 0x5EFC587: ??? (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x5F011B8: g_type_register_fundamental (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x5EE929E: ??? (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x5ED9A1E: ??? (in /usr/lib/i386-linux-gnu/libgobject-2.0.so.0.4600.2)
==5219==    by 0x400EDCF: call_init.part.0 (dl-init.c:72)
==5219==    by 0x400EEDF: call_init (dl-init.c:30)
==5219==    by 0x400EEDF: _dl_init (dl-init.c:120)
==5219==    by 0x4000ACE: ??? (in /lib/i386-linux-gnu/ld-2.21.so)

完整的日志文件: http://pastebin.com/DQxQtnzK

如何解决此问题?我该怎么办?

How do I solve this problem? What should I do?

推荐答案

泄漏不是来自您的代码

以最少的编辑记录注释.

The leaks are not from your code

Transcribing comments with minimal editing.

添加禁止项;它们是由您无法修复的启动代码引起的泄漏",并且可能不会由C ++运行时的开发人员修复.在Mac OS X上,这是一个广泛的问题.启动代码通常会分配许多内存和数十KB的内存. (选项包括--gen-suppressions=all--suppressions=suppressions-file.)

这收集了评论:

FATAL:无法打开禁止文件"suppressions-file"

FATAL: can't open suppressions file "suppressions-file"

哪个人得到了riposte(因为我发表评论时受到时间的压力):

Which got the riposte (since I was under time pressure when I made the comment):

阅读手册.您使用--gen-suppressions=all生成抑制[…].然后编辑文件(在该文件中有一个占位符,用于为每个抑制条目放置一个ID/名称).然后,您可以在随后的--suppressions=name-you-created运行中使用该文件.您可能需要--show-leak-kinds=all,也许还需要其他一些(例如,-leak-check=full).使用valgrind --help,但要注意输出范围很广.

Read the manual. You use --gen-suppressions=all to generate the suppressions […]. Then edit the file (there's a placeholder where you're supposed to put an ID/name for each suppression entry). Then you can use the file in subsequent runs with --suppressions=name-you-created. You may need --show-leak-kinds=all, and maybe some others, too (-leak-check=full, for example). Use valgrind --help, but be aware that the output is fairly extensive.

可行的示例-Mac OS X 10.11.4

来源(mincpp.cpp)

Worked example — Mac OS X 10.11.4

Source (mincpp.cpp)

int main() { return 0; }

编译

g++ -O3 -g -std=c++11 mincpp.cpp -o mincpp

(我通常会使用更多警告选项,但是使用此代码,实际上是没有必要的.)

(I normally use many more warning options, but with this code, there's really no need.)

$ valgrind mincpp
==69167== Memcheck, a memory error detector
==69167== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==69167== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==69167== Command: mincpp
==69167== 
--69167-- UNKNOWN fcntl 97!
--69167-- UNKNOWN fcntl 97! (repeated 2 times)
--69167-- UNKNOWN fcntl 97! (repeated 4 times)
--69167-- UNKNOWN fcntl 97! (repeated 8 times)
--69167-- UNKNOWN fcntl 97! (repeated 16 times)
--69167-- UNKNOWN fcntl 97! (repeated 32 times)
==69167== 
==69167== HEAP SUMMARY:
==69167==     in use at exit: 22,195 bytes in 190 blocks
==69167==   total heap usage: 255 allocs, 65 frees, 27,947 bytes allocated
==69167== 
==69167== LEAK SUMMARY:
==69167==    definitely lost: 4,120 bytes in 2 blocks
==69167==    indirectly lost: 2,288 bytes in 6 blocks
==69167==      possibly lost: 4,880 bytes in 45 blocks
==69167==    still reachable: 2,344 bytes in 12 blocks
==69167==         suppressed: 8,563 bytes in 125 blocks
==69167== Rerun with --leak-check=full to see details of leaked memory
==69167== 
==69167== For counts of detected and suppressed errors, rerun with: -v
==69167== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$

对于这样一个最小的程序,这是很多泄漏!

That's a lot of leakage for such a minimal program!

未知的fcntl消息很麻烦,但看起来几乎没有害处";我可能需要再次重建valgrind.

The unknown fcntl messages are a nuisance but appear to be 'mostly harmless'; I probably need to rebuild valgrind once more.

$ valgrind --gen-suppressions=all --leak-check=full --show-leak-kinds=all mincpp
==69211== Memcheck, a memory error detector
==69211== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==69211== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==69211== Command: mincpp
==69211== 
--69211-- UNKNOWN fcntl 97!
--69211-- UNKNOWN fcntl 97! (repeated 2 times)
--69211-- UNKNOWN fcntl 97! (repeated 4 times)
--69211-- UNKNOWN fcntl 97! (repeated 8 times)
--69211-- UNKNOWN fcntl 97! (repeated 16 times)
--69211-- UNKNOWN fcntl 97! (repeated 32 times)
==69211== 
==69211== HEAP SUMMARY:
==69211==     in use at exit: 22,195 bytes in 190 blocks
==69211==   total heap usage: 255 allocs, 65 frees, 27,947 bytes allocated
==69211== 
==69211== 24 bytes in 1 blocks are still reachable in loss record 5 of 62
==69211==    at 0x1000071FC: malloc_zone_malloc (vg_replace_malloc.c:304)
==69211==    by 0x1005DC1E4: _read_images (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005E19EB: object_setClass (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DABC7: gc_init (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DA8C1: preopt_init (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DA5CA: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005ECC6C: batchFinalizeOnTwoThreads(_malloc_zone_t*, void (*)(auto_zone_cursor*, void (*)(void*, void*), void*), auto_zone_cursor*, unsigned long) (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==69211==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==69211==    by 0x10023789D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==69211==    by 0x1005D907B: _objc_init (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1001DFC93: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==69211== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:_read_images
   fun:object_setClass
   fun:gc_init
   fun:preopt_init
   fun:map_images_nolock
   fun:_ZL25batchFinalizeOnTwoThreadsP14_malloc_zone_tPFvP16auto_zone_cursorPFvPvS3_ES3_ES2_m
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE
   fun:_ZN4dyld36registerImageStateBatchChangeHandlerE17dyld_image_statesPFPKcS0_jPK15dyld_image_infoE
   fun:dyld_register_image_state_change_handler
   fun:_objc_init
   fun:_os_object_init
}
==69211== 24 bytes in 1 blocks are still reachable in loss record 6 of 62
==69211==    at 0x1000071FC: malloc_zone_malloc (vg_replace_malloc.c:304)
==69211==    by 0x1005DC1E4: _read_images (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005E19EB: object_setClass (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DCC96: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DB9B8: _read_images (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005DA5DA: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1005ECC6C: batchFinalizeOnTwoThreads(_malloc_zone_t*, void (*)(auto_zone_cursor*, void (*)(void*, void*), void*), auto_zone_cursor*, unsigned long) (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==69211==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==69211==    by 0x10023789D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==69211==    by 0x1005D907B: _objc_init (in /usr/lib/libobjc.A.dylib)
==69211==    by 0x1001DFC93: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==69211== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:_read_images
   fun:object_setClass
   fun:NXHashInsert
   fun:_read_images
   fun:map_images_nolock
   fun:_ZL25batchFinalizeOnTwoThreadsP14_malloc_zone_tPFvP16auto_zone_cursorPFvPvS3_ES3_ES2_m
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE
   fun:_ZN4dyld36registerImageStateBatchChangeHandlerE17dyld_image_statesPFPKcS0_jPK15dyld_image_infoE
   fun:dyld_register_image_state_change_handler
   fun:_objc_init
   fun:_os_object_init
}
…
==69211== LEAK SUMMARY:
==69211==    definitely lost: 4,120 bytes in 2 blocks
==69211==    indirectly lost: 2,288 bytes in 6 blocks
==69211==      possibly lost: 4,880 bytes in 45 blocks
==69211==    still reachable: 2,344 bytes in 12 blocks
==69211==         suppressed: 8,563 bytes in 125 blocks
==69211== 
==69211== For counts of detected and suppressed errors, rerun with: -v
==69211== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 12 from 12)
$

有时候,我为某些数字 NN 添加--num-callers=NN,但是默认值似乎是我的构建中的12(我认为,在早期版本中该值较小),就足够了.

Sometimes, I add --num-callers=NN for some number NN, but the default seems to be 12 on my build (it was less in earlier versions, I think), which is adequate.

$ valgrind --gen-suppressions=all --leak-check=full --show-leak-kinds=all mincpp 2>./min.suppressions
$ 

编辑min.suppressions

删除以==--开头的行;剩下的就是压制.为抑制添加名称.最终结果类似于:

Edit min.suppressions

Delete lines starting == and --; what's left are suppressions. Add names for the suppressions. End result is similar to:

{
   Mac-OSX-10.11.4-GCC-5.3.0-C++-Suppressions-001
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:_read_images
   fun:object_setClass
   fun:gc_init
   fun:preopt_init
   fun:map_images_nolock
   fun:_ZL25batchFinalizeOnTwoThreadsP14_malloc_zone_tPFvP16auto_zone_cursorPFvPvS3_ES3_ES2_m
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE
   fun:_ZN4dyld36registerImageStateBatchChangeHandlerE17dyld_image_statesPFPKcS0_jPK15dyld_image_infoE
   fun:dyld_register_image_state_change_handler
   fun:_objc_init
   fun:_os_object_init
}
{
   Mac-OSX-10.11.4-GCC-5.3.0-C++-Suppressions-002
   Memcheck:Leak
   match-leak-kinds: reachable
   fun:malloc_zone_malloc
   fun:_read_images
   fun:object_setClass
   fun:NXHashInsert
   fun:_read_images
   fun:map_images_nolock
   fun:_ZL25batchFinalizeOnTwoThreadsP14_malloc_zone_tPFvP16auto_zone_cursorPFvPvS3_ES3_ES2_m
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE
   fun:_ZN4dyld36registerImageStateBatchChangeHandlerE17dyld_image_statesPFPKcS0_jPK15dyld_image_infoE
   fun:dyld_register_image_state_change_handler
   fun:_objc_init
   fun:_os_object_init
}
…
{
   Mac-OSX-10.11.4-GCC-5.3.0-C++-Suppressions-021
   Memcheck:Leak
   match-leak-kinds: definite
   fun:malloc_zone_memalign
   fun:_ZL11addSubclassP10objc_classS0_
   fun:_ZL12realizeClassP10objc_class
   fun:_ZL12realizeClassP10objc_class
   fun:_ZN4dyldL12notifySingleE17dyld_image_statesPK11ImageLoader
   fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader15runInitializersERKNS_11LinkContextERNS_21InitializerTimingListE
   fun:_ZN4dyld24initializeMainExecutableEv
   fun:_ZN4dyld5_mainEPK12macho_headermiPPKcS5_S5_Pm
   fun:_ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
}

使用--suppressions=./min.suppressions

重新运行

Rerun with --suppressions=./min.suppressions

$ valgrind --suppressions=./min.suppressions mincpp
==72028== Memcheck, a memory error detector
==72028== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==72028== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==72028== Command: mincpp
==72028== 
--72028-- UNKNOWN fcntl 97!
--72028-- UNKNOWN fcntl 97! (repeated 2 times)
--72028-- UNKNOWN fcntl 97! (repeated 4 times)
--72028-- UNKNOWN fcntl 97! (repeated 8 times)
--72028-- UNKNOWN fcntl 97! (repeated 16 times)
--72028-- UNKNOWN fcntl 97! (repeated 32 times)
==72028== 
==72028== HEAP SUMMARY:
==72028==     in use at exit: 22,195 bytes in 190 blocks
==72028==   total heap usage: 255 allocs, 65 frees, 27,947 bytes allocated
==72028== 
==72028== LEAK SUMMARY:
==72028==    definitely lost: 0 bytes in 0 blocks
==72028==    indirectly lost: 0 bytes in 0 blocks
==72028==      possibly lost: 0 bytes in 0 blocks
==72028==    still reachable: 0 bytes in 0 blocks
==72028==         suppressed: 22,195 bytes in 190 blocks
==72028== 
==72028== For counts of detected and suppressed errors, rerun with: -v
==72028== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
$

这表明以前报告的错误现在已被抑制.

This shows that the previously reported errors are now suppressed.

这篇关于Valgrind使用空的main显示内存泄漏,不包含头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆