admin管理员组文章数量:1537955
目录
内存泄露
重复释放
错误释放
相关资料
内存泄露
示例代码
// MemoryLeak.cpp : 定义控制台应用程序的入口点。
//
#include <stdlib.h>
#include <string>
char* AllocateMemory(size_t nSize)
{
return new char[nSize];
}
int main()
{
size_t nSize = 16;
char* pszData = AllocateMemory(nSize);
// do something
return 0;
}
编译:g++ -Wall -g MemoryLeak.cpp -o MemoryLeak
执行:valgrind --log-file=MemoryLeak.log --tool=memcheck --leak-check=full -v ./MemoryLeak
MemoryLeak.log内容如下:
==14736== Memcheck, a memory error detector ==14736== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==14736== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==14736== Command: ./MemoryLeak ==14736== Parent PID: 3836 ==14736== ==14736== ==14736== HEAP SUMMARY: ==14736== in use at exit: 16 bytes in 1 blocks ==14736== total heap usage: 2 allocs, 1 frees, 72,720 bytes allocated ==14736== ==14736== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1 ==14736== at 0x4C3089F: operator new[](unsigned long) (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so) ==14736== by 0x108691: AllocateMemory(unsigned long) (MemoryLeak.cpp:9) ==14736== by 0x1086AF: main (MemoryLeak.cpp:15) ==14736== ==14736== LEAK SUMMARY: |
版权声明:本文标题:Linux下通过valgrind定位程序内存问题 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/xitong/1725886457a1047062.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论