如何检测"可能丢失"当没有提供足够的数据时,valgrind中的字节数是多少?

时间:2015-02-17 06:31:33

标签: c++ memory-leaks valgrind

我在我的1500行代码中挖了几天才发现这15个字节(可能丢失)无济于事。 即使我运行以下命令,valgrind也没有提供足够的数据:

valgrind --leak-check=full --show-reachable=yes --track-origins=yes --show-below-main=yes ./myapp

我收到以下报告块:

==3283== 15 bytes in 1 blocks are possibly lost in loss record 1 of 4
==3283==    at 0x402842F: operator new(unsigned int) (vg_replace_malloc.c:255)
==3283==    by 0x40D2A83: std::string::_Rep::_S_create(unsigned int, unsigned int, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==3283==    by 0x40D4CF7: char* std::string::_S_construct<char const*>(char const*, char const*, std::allocator<char> const&, std::forward_iterator_tag) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==3283==    by 0x40D4E65: std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) (in /usr/lib/i386-linux-gnu/libstdc++.so.6.0.16)
==3283==    by 0x804DB22: _GLOBAL__sub_I__ZN7processC2Ei7in_addr (main.cpp:1304)
==3283==    by 0x8050131: __libc_csu_init (in /home/username/myapp-write/src/myapp)
==3283==    by 0x41A60A9: __libc_start_main (libc-start.c:185)
==3283==    by 0x80499C0: ??? (in /home/username/myapp-write/src/myapp)
==3283== 

请问任何人,请告诉我如何检测故障线路?

1 个答案:

答案 0 :(得分:2)

如果您的代码调用 not 设计的替代终止策略,以便由于阻碍展开语义而正确清理自动变量,则不会调用自动变量析构函数。

std::terminate,正如您在评论中提到的那样,不幸的是,他提出了一个这样的条件。终止处理程序的默认操作是调用std::abort,它在自动,线程局部或静态存储持续时间对象上执行 not fire scanup destroy,以及任何假定为动态的vars内存管理会像筛子一样泄漏。

避免以这种方式终止,除非你有非常的充分理由,并且通常很少很好的理由。

祝你好运。