首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Valgrind显示比allocs更多的空闲

Valgrind显示比allocs更多的空闲
EN

Stack Overflow用户
提问于 2020-04-23 23:30:44
回答 1查看 217关注 0票数 1

我在我的程序上运行valgrind,它没有返回内存泄漏。然而,它显示了更多的自由,而不是分配,我真的不确定为什么。

提前感谢

代码语言:javascript
运行
复制
==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)
EN

回答 1

Stack Overflow用户

发布于 2020-04-24 22:04:57

您可以使用--trace-malloc=yes选项检查分配了哪些地址。我建议不要在大型应用程序中使用它!

例如,使用这个小应用程序

代码语言:javascript
运行
复制
int main()
{
   int* pi = new int;
   delete pi;
}

我得到了

代码语言:javascript
运行
复制
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)

几个注意事项

  1. 你可能想要运行valgrind而不是vg-in-place,除非你自己构建Valgrind。
  2. 函数名是乱码。如果想要查看未损坏的名称,可以运行

代码语言:javascript
运行
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61391066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档