在编写嵌入式代码的时候,由于在全入市,有时候比较的注意内存资源的使用。在inux环境下,我使用了下面的较为简单的内存检查系统。
具体不多说了,思想比较的简单,就是记录下每次new分配资源的时候,记录下相关的信息,然后在调用delete释放资源的时候,
找到分配的时候对应的记录,将其删除。如果最后,每次new时记录的信息,都在delete的时候被删除了,那么就认为没有内存泄露。
如果,到最后程序运行完时候,还有一些调用new时分配的存放的记录还没有被删除,那么就认为出现了内存泄露。并将分配资源却没有
被释放的那块内存地址和分配内存时的文件和代码位置,方便我们进行调试。
头文件:
#ifndef MEMCHECK_H
#define MEMCHECK_H
#include <cstddef> // for size_t
// Use the new operator (both scalar and array versions)
void* operator new(size_t, const char*, long);
void* operator new[](size_t, const char*, long);
#define new new (__FILE__, __LINE__)
extern bool traceFlag;
#define TRACE_ON() traceFlag = true
#define TRACE_OFF() traceFlag = false
extern bool activeFlag;
#define MEM_ON() activeFlag = true
#define MEM_OFF() activeFlag = false
#endif
源文件:
#include <cstdio>
#include <cstdlib>
#include <cassert>
#include <cstddef>
#undef new
//global flags set by macros in MemCheck.h
bool traceFlag = true;
bool activeFlag = false;
namespace
{
//memory map entry type
struct Info
{
void *ptr;
const char *file;
long line;
};
//Memory map data
const size_t MAXPTRS = 10000u;
Info memMap[MAXPTRS];
size_t nptrs = 0;
//Searches the map for an address
int findPtr(void *p)
{
for(size_t i = 0; i < nptrs; ++i)
if(memMap[i].ptr == p)
return i;
return -1;
}
void delPtr(void* p)
{
int pos = findPtr(p);
assert(pos >= 0);
//Remove pointer from map
for (size_t i = pos; i < nptrs -1; ++i)
{
memMap[i] = memMap[i+1];
}
--nptrs;
}
//Dummy type for static destructor
struct Sentinel
{
Sentinel(){}
~Sentinel()
{
if (nptrs > 0)
{
printf("Leaked memory at:\n");
for(size_t i = 0; i < nptrs; ++i)
{
printf("\t%p (file: %s, line %ld)\n", memMap[i].ptr, memMap[i].file, memMap[i].line);
}
}
else
printf("No user memory leaks!\n");
}
};
// static dummy object
Sentinel s;
}// end of anonymous namespace
// Overload scalar new
void* operator new(size_t siz, const char* file, long line)
{
void *p = malloc(siz);
if (activeFlag)
{
if (nptrs == MAXPTRS)
{
printf("Memory map too small (increase MAXPTRS)\n");
exit(1);
}
memMap[nptrs].ptr = p;
memMap[nptrs].file = file;
memMap[nptrs].line = line;
++nptrs;
}
if (traceFlag)
{
printf("Allocated %u bytes at address %p", siz, p);
printf("(file: %s, line: %ld)\n", file, line);
}
return p;
}
//Overload array new
void* operator new[](size_t siz, const char* file, long line)
{
return operator new(siz, file, line);
}
//Override scalar delete
void operator delete(void* p)
{
if (findPtr(p) >= 0)
{
free(p);
assert(nptrs > 0);
delPtr(p);
if (traceFlag)
{
printf("Deleted memory at address %p\n", p);
}
}
else if(!p && activeFlag)
printf("Attempt to delete unknown pointer: %p\n", p);
}
//Override array delete
void operator delete[](void *p)
{
operator delete(p);
}
简单实用示例:
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;
#include "MemCheck.h"
class Foo{
char *s;
public:
Foo(const char *s)
{
this->s = new char[strlen(s) + 1];
strcpy(this->s, s);
}
~Foo()
{
delete[] s;
}
};
void main(void)
{
MEM_ON();
cout<<"hello"<<endl;
int *p = new int;
delete p;
int *q = new int[3];
delete[] q;
int *r;
delete r;
vector<int> v;
v.push_back(1);
Foo s("good bye");
MEM_OFF();
}