国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > C++ > 正文

一個(gè)簡(jiǎn)單的C++內(nèi)存泄漏檢測(cè)程序

2019-11-11 05:47:41
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

基本思路就是跟蹤程序中的malloc和free調(diào)用,然后檢測(cè)是否有漏掉的free調(diào)用,代碼如下:

MemCheck.h

#ifndef __MEM_CHECK_H__#define __MEM_CHECK_H__#include <cstddef>void* _Malloc_Trace_Call_(std::size_t MemSize, const char* SourceFile, int LineNumber, std::size_t Alignment = 0);void _Free_Trace_Call_(void* Ptr, std::size_t Alignment = 0);void DumpMemoryInfo();#undef malloc#define malloc(Size) _Malloc_Trace_Call_(Size, __FILE__, __LINE__)#undef free#define free(Ptr) _Free_Trace_Call_(Ptr)#undef _mm_malloc#define _mm_malloc(Size, Alignment) _Malloc_Trace_Call_(Size, __FILE__, __LINE__, Alignment)#undef _mm_free#define _mm_free(Ptr) _Free_Trace_Call_(Ptr, 1)#endifMemCheck.cpp

#include <iostream>#include <cstdlib>#include <map>#include <mutex>#include "MemCheck.h"#undef malloc#undef free#undef _mm_malloc#undef _mm_freenamespace{	struct MemInfo	{		MemInfo(std::size_t _Size, const char* _SourceFile, int _LineNumber)		:Size(_Size)		, SourceFile(_SourceFile)		, LineNumber(_LineNumber)		{		}		std::size_t	Size;		const char*	SourceFile;		int		LineNumber;	};	std::map<void*, MemInfo> g_MemInfoMap;	std::size_t g_TotalMemAllocation = 0;	std::size_t g_TotalMemDeallocation = 0;	std::mutex g_Mutex;}void* _Malloc_Trace_Call_(std::size_t MemSize, const char* SourceFile, int LineNumber, std::size_t Alignment ){	void* Ptr = nullptr;	std::lock_guard<std::mutex> Locker(g_Mutex);	if (Alignment > 0)	{		Ptr = _aligned_malloc(MemSize, Alignment);	}	else	{		Ptr = malloc(MemSize);	}	if (Ptr != nullptr)	{		g_MemInfoMap.emplace(Ptr, MemInfo(MemSize, SourceFile, LineNumber));		g_TotalMemAllocation += MemSize;		return Ptr;	}	else	{		throw std::bad_alloc();	}}void _Free_Trace_Call_(void* Ptr, std::size_t Alignment ){	std::lock_guard<std::mutex> Locker(g_Mutex);	auto Iter = g_MemInfoMap.find(Ptr);	if (Iter != g_MemInfoMap.cend())	{		if (Alignment > 0)			_aligned_free(Ptr);		else			free(Ptr);		g_TotalMemDeallocation += Iter->second.Size;		g_MemInfoMap.erase(Iter);	}	else	{		std::cout << "Attempt to delete invalid ptr: " << std::ios::hex << Ptr << std::endl;	}}void DumpMemoryInfo(){	using std::cout;	using std::endl;	cout << "Total memeory allocation: " << g_TotalMemAllocation << " Bytes" << endl;	cout << "Total memory deallocation: " << g_TotalMemDeallocation << " Bytes" << endl;	if (g_MemInfoMap.size() > 0)	{		cout << "Memory leak detected: " << g_TotalMemAllocation - g_TotalMemDeallocation << " Bytes" << endl;		for (auto const& Iter : g_MemInfoMap)		{			auto const& Info = Iter.second;			cout << Info.SourceFile << "/tLine: " << Info.LineNumber << "/tSize: " << Info.Size 				 << "/tAddress: " << std::ios::hex << Iter.first << endl;		}	}}幾個(gè)小細(xì)節(jié):

除了基本的malloc和free,還需要跟蹤對(duì)aligned malloc的調(diào)用,因?yàn)橛袝r(shí)候必須使用對(duì)齊的內(nèi)存,這一部分不能漏掉。

跟蹤函數(shù)需要加鎖防止多線程環(huán)境下出現(xiàn)bug

測(cè)試代碼:

#include "MemCheck.h"using namespace std;int main(){	void* p1 = malloc(10);	void* p2 = malloc(20);	void* p3 = malloc(30);	free(p3);	free(p2);	DumpMemoryInfo();	return 0;}

輸出:

Total memeory allocation: 60 BytesTotal memory deallocation: 50 BytesMemory leak detected: 10 Bytesmain.cpp    Line: 6        Size: 10        Address: 204800E66128

在使用的時(shí)候,需要把MemCheck.h頭文件放在所有的頭文件最后。因?yàn)槲覀冎幌霗z測(cè)自己程序中的內(nèi)存泄漏,不需要其它庫(kù)中的內(nèi)存分配信息。

這個(gè)程序只能檢測(cè)malloc和free的調(diào)用,對(duì)于new和delete,情況會(huì)棘手一些。對(duì)于new的調(diào)用,可以像malloc一樣用宏替換掉,然后調(diào)用自定義的Operator new。比如可以這么寫:

void* operator new(std::size_t MemSize, const char* SourceFile, std::size_t LineNumber);void* operator new[](std::size_t MemSize, const char* SourceFile, std::size_t LineNumber);#define new new (__FILE__, __LINE__)對(duì)于delete,由于目前C++ 的operator delete只能傳遞一個(gè)指針參數(shù),所以只能重載全局的::operator delete。但如果重載了全局delete,所有的調(diào)用都會(huì)被影響,例如STL容器的默認(rèn)分配器std::allocator中就使用了全局::operator delete,這樣其他庫(kù)中的內(nèi)存信息就會(huì)干擾檢測(cè)結(jié)果。

一種不太雅觀的方法就是所有需要檢測(cè)new和delete的Class都繼承一個(gè)基類,然后這個(gè)基類重載new和delete記錄分配信息,這樣就不會(huì)影響到其他庫(kù)


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 麻城市| 绥棱县| 安吉县| 曲靖市| 德江县| 安达市| 慈溪市| 邳州市| 古浪县| 穆棱市| 蕉岭县| 桂东县| 隆子县| 碌曲县| 花莲市| 合阳县| 杭锦旗| 彭水| 曲松县| 上林县| 延寿县| 榆林市| 拉孜县| 彭阳县| 彰武县| 济宁市| 舟曲县| 百色市| 平塘县| 潞西市| 波密县| 余庆县| 连城县| 沛县| 宣威市| 锦州市| 鹤峰县| 仲巴县| 湟中县| 遵义县| 湟中县|