目录
内存泄露
示例代码
// 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: |