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

首頁 > 編程 > C++ > 正文

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

2019-11-11 04:20:10
字體:
供稿:網(wǎng)友

基本思路就是跟蹤程序中的malloc和free調(diào)用,然后檢測是否有漏掉的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;		}	}}幾個小細(xì)節(jié):

除了基本的malloc和free,還需要跟蹤對aligned malloc的調(diào)用,因為有時候必須使用對齊的內(nèi)存,這一部分不能漏掉。

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

測試代碼:

#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

在使用的時候,需要把MemCheck.h頭文件放在所有的頭文件最后。因為我們只想檢測自己程序中的內(nèi)存泄漏,不需要其它庫中的內(nèi)存分配信息。

這個程序只能檢測malloc和free的調(diào)用,對于new和delete,情況會棘手一些。對于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__)對于delete,由于目前C++ 的operator delete只能傳遞一個指針參數(shù),所以只能重載全局的::operator delete。但如果重載了全局delete,所有的調(diào)用都會被影響,例如STL容器的默認(rèn)分配器std::allocator中就使用了全局::operator delete,這樣其他庫中的內(nèi)存信息就會干擾檢測結(jié)果。

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


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 南岸区| 洪江市| 巴青县| 睢宁县| 吉水县| 木里| 溧水县| 宕昌县| 桦甸市| 新巴尔虎左旗| 朔州市| 含山县| 玉环县| 含山县| 胶南市| 云和县| 扎鲁特旗| 龙井市| 青龙| 大城县| 厦门市| 泸溪县| 东光县| 农安县| 宜丰县| 宜黄县| 开化县| 原平市| 扎鲁特旗| 关岭| 子洲县| 鹤山市| 台前县| 鹤岗市| 高雄市| 灵山县| 和平区| 宜兴市| 忻州市| 碌曲县| 伊宁市|