我在程序上运行valgrind,它没有返回任何内存泄漏。但是,它显示的释放空间多于分配空间,并且我不确定为什么。
提前感谢
==4234== HEAP SUMMARY:
==4234== in use at exit: 0 bytes in 0 blocks
==4234== total heap usage: 304 allocs, 542 frees, 81,820 bytes allocated
==4234==
==4234== All heap blocks were freed -- no leaks are possible
==4234==
==4234== For counts of detected and suppressed errors, rerun with: -v
==4234== Use --track-origins=yes to see where uninitialised values come from
==4234== ERROR SUMMARY: 3555 errors from 13 contexts (suppressed: 0 from 0)
您可以使用--trace-malloc=yes
选项检查正在分配的地址。我建议不要在大型应用程序上使用它!
例如,对于这个小应用程序
int main()
{
int* pi = new int;
delete pi;
}
我知道
paulf> ./vg-in-place --trace-malloc=yes ../vg_examples/noleak
==88971== Memcheck, a memory error detector
==88971== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==88971== Using Valgrind-3.16.0.GIT and LibVEX; rerun with -h for copyright info
==88971== Command: ../vg_examples/noleak
==88971==
--88971-- _Znwm(4) = 0x5800040
--88971-- _ZdlPv(0x5800040)
==88971==
==88971== HEAP SUMMARY:
==88971== in use at exit: 0 bytes in 0 blocks
==88971== total heap usage: 1 allocs, 1 frees, 4 bytes allocated
==88971==
==88971== All heap blocks were freed -- no leaks are possible
==88971==
==88971== For lists of detected and suppressed errors, rerun with: -s
==88971== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
一些注意事项
valgrind
而不是vg-in-place
。>paulf> ./vg-in-place --trace-malloc=yes ../vg_examples/noleak 2>&1 | /usr/bin/c++filt
[snip]
--89034-- operator new(unsigned long)(4) = 0x5800040
--89034-- operator delete(void*)(0x5800040)