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

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

一個簡單的C++內存泄漏檢測程序

2019-11-11 04:59:56
字體:
來源:轉載
供稿:網(wǎng)友

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

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;		}	}}幾個小細節(jié):

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

跟蹤函數(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頭文件放在所有的頭文件最后。因為我們只想檢測自己程序中的內存泄漏,不需要其它庫中的內存分配信息。

這個程序只能檢測malloc和free的調用,對于new和delete,情況會棘手一些。對于new的調用,可以像malloc一樣用宏替換掉,然后調用自定義的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,所有的調用都會被影響,例如STL容器的默認分配器std::allocator中就使用了全局::operator delete,這樣其他庫中的內存信息就會干擾檢測結果。

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


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

圖片精選

主站蜘蛛池模板: 丘北县| 太仓市| 临清市| 阿瓦提县| 贵南县| 淳化县| 望谟县| 古交市| 白山市| 曲阜市| 井研县| 安西县| 城步| 台前县| 汉川市| 常宁市| 利川市| 象山县| 周至县| 元阳县| 阿拉善盟| 古丈县| 民丰县| 榆树市| 个旧市| 隆林| 博野县| 大丰市| 乐安县| 毕节市| 井研县| 清镇市| 博客| 翼城县| 太湖县| 海伦市| 阳曲县| 微山县| 泸州市| 化德县| 平远县|