系统相关
首页 > 系统相关> > C++ 内存泄漏

C++ 内存泄漏

作者:互联网

文章目录

前言

一、准备工作

1、工具安装

2、目录添加

右键工程Properties C/C++ General Additional_Include_Directories 右键工程Properties Linker General Additional_Library_Directories

3、信息配置

    ReportFile =.\memory_leak_report.txt 
    ReportTo = both
    #include <vld.h>

二、基础测试

1、简单尝试

#include "vld.h"

int main() {
	int *p = new int;
	*p = 0x09ABCDEF;
	return 0;
}
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 1 at 0x0000000013065440: 4 bytes ----------
  Leak Hash: 0xD58AB4EF, Count: 1, Total 4 bytes
  Call Stack (TID 17172):
    MSVCR120D.dll!operator new()
    e:\dreamrivakes\trunk\program\other\memoryleaktester\memoryleaktester\memoryleaktester\main.cpp (78): MemoryLeakTester.exe!main() + 0xA bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): MemoryLeakTester.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    EF CD AB 09                                                  ........ ........
Visual Leak Detector detected 1 memory leak (56 bytes).

2、报告分析

3、某些疑惑

#include "vld.h"

int main() {
	char *p1 = new char;
	*p1 = 0xAB;

	short *p2 = new short;
	*p2 = 0xABCD;

	int *p3 = new int;
	*p3 = 0x09ABCDEF;

	long long *p4 = new long long;

	return 0;
}
WARNING: Visual Leak Detector detected memory leaks!

---------- Block 1 at 0x00000000E1167080: 1 bytes ----------
  Leak Hash: 0xE21F7247, Count: 1, Total 1 bytes
  Call Stack (TID 16576):
    ...
  Data:
    AB
    
---------- Block 2 at 0x00000000E1165BF0: 2 bytes ----------
  Leak Hash: 0xE58559DC, Count: 1, Total 2 bytes
  Call Stack (TID 16576):
    ...
  Data:
    CD AB

---------- Block 3 at 0x00000000E1165C60: 4 bytes ----------
  Leak Hash: 0xB0A3AA1A, Count: 1, Total 4 bytes
  Call Stack (TID 16576):
    ...
  Data:
    EF CD AB 09

---------- Block 4 at 0x00000000E1165440: 8 bytes ----------
  Leak Hash: 0x49D5C84C, Count: 1, Total 8 bytes
  Call Stack (TID 16576):
    ...
  Data:
    CD CD CD CD    CD CD CD CD

Visual Leak Detector detected 4 memory leaks (223 bytes).
    (223 - 15) / 4 = 52

4、解决泄漏

#include "vld.h"

int main() {
	int *p = new int;
	delete p;
	return 0;
}
    No memory leaks detected.
    Visual Leak Detector is now exiting.

三、常见内存泄漏

1、虚析构

    i. 为什么基类对象的析构函数一般都要声明 virtual 关键字?
    ii. 虚析构的作用和原理是什么?
#include "vld.h"

using namespace std;

// 基类
class LeakBaseObject {
public:
	LeakBaseObject() {
		printf("LeakBaseObject Construction!\n");
		
	}
	~LeakBaseObject() {
		printf("LeakBaseObject Destruction!\n");
	}
};

// 派生类
class LeakObject : public LeakBaseObject {
public:
	LeakObject() {
		printf("LeakObject Construction!\n");
		p = new int[100];
	}
	~LeakObject() {
		printf("LeakObject Destruction!\n");
		if (p) {
			delete[] p;
			p = nullptr;
		}
	}
private:
	int *p;
};

int main() {
    // 基类指针指向派生类
	LeakBaseObject *pLBObj = new LeakObject();
	delete pLBObj;
}
LeakBaseObject Construction!
LeakObject Construction!
LeakBaseObject Destruction!
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 2 at 0x000000004671BB20: 400 bytes ----------
  Leak Hash: 0x1B693661, Count: 1, Total 400 bytes
  Call Stack (TID 8688):
    ... ...
  Data:
    ... ...

Visual Leak Detector detected 1 memory leak (452 bytes).
class LeakBaseObject {
public:
	LeakBaseObject() {
		printf("LeakBaseObject Construction!\n");
	}
	virtual ~LeakBaseObject() {
		printf("LeakBaseObject Destruction!\n");
	}
};

2、STL容器泄漏

#include "vld.h"

using namespace std;

// 基类
class LeakBaseObject {
public:
	LeakBaseObject() {
		printf("LeakBaseObject Construction!\n");
		
	}
	~LeakBaseObject() {
		printf("LeakBaseObject Destruction!\n");
	}
};

// 派生类
class LeakObject : public LeakBaseObject {
public:
	LeakObject() {
		printf("LeakObject Construction!\n");
	}
	~LeakObject() {
		printf("LeakObject Destruction!\n");
	}
private:
	vector<int>    m_kVector;
};

int main() {
    // 基类指针指向派生类
	LeakBaseObject *pLBObj = new LeakObject();
	delete pLBObj;
}
WARNING: Visual Leak Detector detected memory leaks!
---------- Block 2 at 0x00000000781758C0: 16 bytes ----------
  Leak Hash: 0x211451AF, Count: 1, Total 16 bytes
  Call Stack (TID 20216):
    MSVCR120D.dll!operator new()
    d:\microsoft visual studio 12.0\vc\include\xmemory0 (848): MemoryLeakTester.exe!std::_Wrap_alloc<std::allocator<std::_Container_proxy> >::allocate()
    d:\microsoft visual studio 12.0\vc\include\vector (624): MemoryLeakTester.exe!std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >::_Alloc_proxy() + 0xF bytes
    d:\microsoft visual studio 12.0\vc\include\vector (603): MemoryLeakTester.exe!std::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >::_Vector_alloc<0,std::_Vec_base_types<int,std::allocator<int> > >() + 0xA bytes
    d:\microsoft visual studio 12.0\vc\include\vector (681): MemoryLeakTester.exe!std::vector<int,std::allocator<int> >::vector<int,std::allocator<int> >()
    e:\dreamrivakes\trunk\program\other\memoryleaktester\memoryleaktester\memoryleaktester\main.cpp (25): MemoryLeakTester.exe!LeakObject::LeakObject()
    e:\dreamrivakes\trunk\program\other\memoryleaktester\memoryleaktester\memoryleaktester\main.cpp (64): MemoryLeakTester.exe!main() + 0x30 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): MemoryLeakTester.exe!mainCRTStartup()
    KERNEL32.DLL!BaseThreadInitThunk() + 0x14 bytes
    ntdll.dll!RtlUserThreadStart() + 0x21 bytes
  Data:
    ...

Visual Leak Detector detected 1 memory leak (68 bytes).
vector构造函数 _Vector_alloc构造函数 _Alloc_proxy _Alproxy.allocate _Alloc::allocate _Allocate
template<class _Ty> inline                                            //i
_Ty *_Allocate(size_t _Count, _Ty *)
{	
    // allocate storage for _Count elements of type _Ty
	void *_Ptr = 0;
	if (_Count == 0)
		;
	else if (((size_t)(-1) / sizeof (_Ty) < _Count)                  // ii
		|| (_Ptr = ::operator new(_Count * sizeof (_Ty))) == 0)      // iii
		_Xbad_alloc();	// report no memory
	return ((_Ty *)_Ptr);
}

3、Protobuf的内存泄漏

	google::protobuf::ShutdownProtobufLibrary();

标签:泄漏,Leak,bytes,C++,内存,new,LeakBaseObject
来源: https://blog.51cto.com/u_15239535/2836986