标签:检查内存泄漏
1.分配空间
2.记录内存块信息
3.调用构造函数(类型萃取)
#include<iostream>
#include<string>
#include<list>
#include<assert.h>
using namespace std;
struct BlockInfo
{
void* _ptr;
string _file;
int _line;
BlockInfo(void *ptr, const char*file, int line)
:_ptr(ptr)
,_file(file)
,_line(line)
{}
};
list<BlockInfo> BlockList;
void* Alloc(size_t size, const char*file, int line)
{
void* ptr = malloc(size);
if (ptr)
{
BlockInfo b(ptr, file, line);
BlockList.push_back(b);
}
return ptr;
}
void Dalloc(void *ptr)
{
free(ptr);
//[)
list<BlockInfo>::iterator it = BlockList.begin();
while (it != BlockList.end())
{
if (it->_ptr == ptr)
{
BlockList.erase(it);
return;
}
++it;
}
assert(false);
}
void Print()
{
cout << "内存泄漏的内存块" << endl;
list<BlockInfo>::iterator it = BlockList.begin();
while (it != BlockList.end())
{
printf("ptr:%p file:%s line:%d\n", it->_ptr, it->_file.c_str(), it->_line);
++it;
}
}
template<class T>
void *__NEW(size_t size, const char* file, int line)
{
void *ptr=Alloc(size, file, line);
if (TypeTraits <T>::__IsPODType().Get())
return ptr;
else
return new(ptr) T;
}
template<class T>
void __DELETE(T *ptr)
{
if (!TypeTraits <T>::__IsPODType().Get())
ptr->~T();
Dalloc(ptr);
}
template<class T>
void *__NEW_ARRAY(size_t size, int num,const char* file, int line)
{
void *ptr = Alloc(size, file, line);
*((int*)ptr) = num;
ptr = (void*)((int)ptr + 4);
T*cur = (T*)ptr;
if (!TypeTraits <T>::__IsPODType().Get())
{
for (int i = 0; i < num; i++)
{
new(cur)T;
++cur;
}
}
return ptr;
}
template<class T>
void __DELETE_ARRAY(void* ptr)
{
int num = *((int*)ptr - 1);
T* cur = (T*)ptr;
if (!TypeTraits <T>::__IsPODType().Get())
{
for (int i = 0; i < num; i++)
{
cur->~T();
++cur;
}
}
Dalloc((void*)((int)ptr-4));
}
struct __TrueType
{
bool Get()
{
return true;
}
};
struct __FalseType
{
bool Get()
{
return false;
}
};
template <class _Tp>
struct TypeTraits
{
typedef __FalseType __IsPODType;
};
template <>
struct TypeTraits< bool>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< char>
{
typedef __TrueType __IsPODType;
};
template <>
struct TypeTraits< int>
{
typedef __TrueType __IsPODType;
};
#define NEW(type) __NEW<type>(sizeof(type),__FILE__,__LINE__)
#define DELETE(type,ptr) __DELETE<type>(ptr)
#define NEW_ARRAY(type,num) __NEW_ARRAY<type>(sizeof(type)*num+4,num,__FILE__,__LINE__)
#define DELETE_ARRAY(type,ptr) __DELETE_ARRAY<type>(ptr)//#include<iostream>
//using namespace std;
//#include"Memory.hpp"
//基本类型
//void Test1()
//{
// int *p1 =(int*)NEW(sizeof(int));
// int *p2 = (int*)NEW(sizeof(int));
// int *p3 = (int*)NEW(sizeof(int));
// DELETE(p1);
// DELETE(p2);
// DELETE(p3);
//}
//自定义类型
//void Test2()
//{
// string *s1 =(string*) NEW(string);
// *s1 = "abcd";
// cout << s1->c_str() << endl;
// DELETE(string,s1);
//}
//基本类型数组
//void Test3()
//{
// int *p1 = (int*)NEW_ARRAY(int, 10);
// *p1 = 2;
// p1[1] = 3;
// int *p2 = (int*)NEW_ARRAY(int, 10);
// DELETE_ARRAY(int, p1);
// DELETE_ARRAY(int, p2);
//}
//自定义类型数组
//void Test4()
//{
// string* p1 = (string*)NEW_ARRAY(string, 5);
// p1[0] = "abc";
// p1[1] = "def";
// p1[2] = "xxx";
// p1[3] = "lll";
// p1[4] = "www";
// DELETE_ARRAY(string, p1);
//}
//void Test5()
//{
//
// int *p1 = (int*)NEW_ARRAY(int, 10);
// int *p2= (int*)NEW_ARRAY(int, 10);
// int *p3 = (int*)NEW_ARRAY(int, 10);
// int *p4 = (int*)NEW_ARRAY(int, 10);
// p1[0] = 1;
// DELETE_ARRAY(int, p1);
// DELETE_ARRAY(int, p2);
// DELETE_ARRAY(int, p3);
// DELETE_ARRAY(int, p4);
//}
//int main()
//{
// Test4();
// //Test5();
// //登记一个函数在main函数执行结束以后执行,函数限制void Func(void)
// atexit(Print);
// system("pause");
// return 0;
//}
本文出自 “小止” 博客,请务必保留此出处http://10541556.blog.51cto.com/10531556/1728574
标签:检查内存泄漏
原文地址:http://10541556.blog.51cto.com/10531556/1728574