关于重载new operator,Effective C++ 中给出了new/delete 的几点理由:
- 检测代码中的内存错误
- 优化性能
- 获得内存使用的统计数据
除了基本的重载new operator之外,你可以增加参数,提高函数的灵活性。比如:
void* operator new(size_t size, const char* file, int line); // 其返回的指针必须能被普通的 ::operator delete(void*) 释放
void operator delete(void* p, const char* file, int line); // 这个函数只在析构函数抛异常的情况下才会被调用
在使用时,
Foo* p = new (__FILE, __LINE__) Foo; // 这样能跟踪是哪个文件,哪一行代码分配的内存
我们也可以用宏替换 new 来节省打字。“检测内存错误”和“统计内存使用情况”通常会用这种方式重载。
class MyObj
{
public:
MyObj():m_i(0), m_c('c'),m_ii(1){}
MyObj& operator=(const MyObj& obj)
{
if (this != &obj)
{
this->m_i = obj.m_c;
this->m_c = obj.m_c;
this->m_ii = obj.m_ii;
}
return *this;
}
private:
int m_i;
char m_c;
int m_ii;
};
class MyObjContainer
{
public:
MyObjContainer():m_i(1),m_pObj(NULL){}
MyObjContainer(MyObj* pObj):m_i(0),m_pObj(pObj){}
MyObjContainer& operator=(const MyObjContainer& obj)
{
if (this != &obj)
{
this->m_i = obj.m_i;
this->m_pObj = obj.m_pObj;
}
return *this;
}
void* operator new(size_t size, new_handler handle = 0)
{
if (!size)
size = 1;
while (1){
void* pointer = malloc(size);
if (pointer != NULL)
{
cout << "Dear, we have new " << size << " memory" << endl;
return pointer;
}
//new_handler global_handler = set_new_handler(0);
set_new_handler(handle);
if (handle)
{
(*handle)();
return 0;
}
else
throw bad_alloc();
}
}
void operator delete(void* pointer)
{
if (NULL == pointer)
return;
free(pointer);
}
void destroy() {
if (m_pObj != NULL)
delete m_pObj;
delete this;
}
private:
int m_i;
MyObj* m_pObj;
int m_arr[1024*1024*510];
~MyObjContainer()
{
}
};
void no_memory()
{
cout << "OMG, no memory!!!!" << endl;
}
void private_decons()
{
MyObj* pMy = new MyObj();
MyObjContainer* pCons = new(no_memory) MyObjContainer(pMy);
if (pCons != NULL)
{
pCons->destroy();
pCons = NULL;
}
else
delete pMy;
}
注意:
- 用系统默认的 malloc() 分配的内存要交给系统默认的 free() 去释放;
- 用系统默认的 new 表达式创建的对象要交给系统默认的 delete 表达式去析构并释放;
- 用系统默认的 new[] 表达式创建的对象要交给系统默认的 delete[] 表达式去析构并释放;
- 用系统默认的 ::operator new() 分配的的内存要交给系统默认的 ::operator delete() 去释放;
- 用 placement new 创建的对象要用 placement delete 去析构;
最近看了《C++ In Action》,其中有一节讲述overloading operator new,内容还是蛮精彩的,其中Tracing memory leak印象颇深。遂记录下来。
class Tracer
{
private:
class Entry
{
public:
Entry (char const * file, int line)
: _file (file), _line (line)
{}
Entry ()
: _file (0), _line (0)
{}
char const * File () const { return _file; }
int Line () const { return _line; }
private:
char const * _file;
int _line;
};
class Lock
{
public:
Lock (Tracer & tracer)
: _tracer (tracer)
{
_tracer.lock ();
}
~Lock ()
{
_tracer.unlock ();
}
private:
Tracer & _tracer;
};
// friend class Lock;
public:
Tracer ();
~Tracer ();
void Add (void * p, char const * file, int line);
void Remove (void * p);
void Dump ();
static bool Ready;
private:
void lock () { _lockCount++; }
void unlock () { _lockCount--; }
private:
typedef std::map<void *, Entry>::iterator iterator;
std::map<void *, Entry> _map;
int _lockCount;
};
bool Tracer::Ready = false;
Tracer::Tracer():_lockCount(0)
{
Ready = true;
}
Tracer::~Tracer()
{
Ready = false;
Dump();
}
void Tracer::Dump()
{
if (_map.size() != 0)
{
cout << _map.size() << " memory leaks detected" << endl;
for (iterator itor = _map.begin(); itor != _map.end(); ++itor)
{
char const* file = itor->second.File();
int line = itor->second.Line();
cout << file << ", " << line << endl;
}
}
}
void Tracer::Add(void *p, const char *file, int line)
{
if (_lockCount > 0)
return;
Tracer::Lock lock(*this);
_map[p] = Entry(file, line);
}
void Tracer::Remove(void *p)
{
if (_lockCount > 0)
return;
Tracer::Lock lock(*this);
iterator itor = _map.find(p);
if (itor != _map.end())
_map.erase(itor);
}
/*extern*/ Tracer gNewTracer;
void* operator new (size_t size, const char* file, int line)
{
void* p = malloc(size);
if (Tracer::Ready)
gNewTracer.Add(p, file, line);
return p;
}
void operator delete(void* p, const char* file, int line)
{
if (Tracer::Ready)
gNewTracer.Remove(p);
free(p);
}
void* operator new (size_t size)
{
void* p = malloc(size);
if (Tracer::Ready)
gNewTracer.Add(p, "?", 0);
return p;
}
void operator delete(void* p)
{
if (Tracer::Ready)
gNewTracer.Remove(p);
free(p);
}
#define new new(__FILE__, __LINE__)
当然这段code中Locker可依据具体实际应用而改变。。。
参考文献:
不要重载全局operator new http://blog.csdn.net/Solstice/article/details/6198937
Effective C++
C++ In Action