当没有提供足够的数据时,如何在 valgrind 中检测 "possibly lost" 字节?

How to detect "possibly lost" bytes in valgrind when no sufficient data are provided?

我已经在我的 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== 

谁能告诉我如何检测故障线路?

如果您的代码调用的替代终止策略不是旨在正确清理自动变量的替代策略,因为阻碍展开语义,您的自动变量析构函数将不会被调用。

std::terminate, as you mentioned in-comment you were using, unfortunately ponies up one such condition. The default action for the termination handler is to invoke std::abort,它 not 对自动、线程本地或静态存储持续时间对象以及任何采用动态内存管理的此类变量触发清理破坏会像筛子一样漏水。

避免以这种方式终止,除非你有非常很好的理由,一般来说很少很好的理由它。

祝你好运。