2010-04-19 89 views
1

我正在使用Windows上的許多外部庫的項目。 我得到了內存泄漏的問題:我通過覆蓋operator new/new []和delete/delete []來檢測到許多內存泄漏。問題是我知道有多少內存塊被泄漏,但不知道在哪裏找到它們,在被忽略的函數中,我可以記錄分配的內存塊的大小和位置,沒有堆棧跟蹤。C++內存泄漏檢測方法

所以要處理它,我想我也需要記錄堆棧跟蹤(但怎麼?),或者有什麼方法可以找到哪些代碼導致內存泄漏?

非常感謝您的幫助。

+0

查看http://stackoverflow.com/questions/105659/how-can-one-grab-a-stack-trace-in-c – 2010-04-19 11:25:44

回答

3

我用下面的方式與在其文件和行信息,其分配每個存儲塊提供new

void operator delete(void *p, const char* filename, int line); 
void operator delete(void *p, const char* filename, int line, const std::nothrow_t&); 
void operator delete[](void *p, const char* filename, int line); 
void operator delete[](void *p, const char* filename, int line, const std::nothrow_t&); 

void *operator new(std::size_t n, const char* filename, int line); 
void *operator new(std::size_t n, const std::nothrow_t&, const char* filename, int line); 
void *operator new[](std::size_t n, const char* filename, int line); 
void *operator new[](std::size_t n, const std::nothrow_t&, const char* filename, int line); 

#define new foo_new 
#define foo_new new(__FILE__, __LINE__) 
+0

雖然這對第三方庫沒有任何幫助。 – Hasturkun 2010-04-19 10:14:00