【问题标题】:C++ memory leak detecting methodC++内存泄漏检测方法
【发布时间】:2010-04-19 10:01:17
【问题描述】:

我正在开发一个在 Windows 上使用许多外部库的项目。 我遇到了内存泄漏问题:我通过覆盖 operator new/new[] 和 delete/delete[] 检测到许多内存泄漏。问题是我知道有多少内存块被泄漏,但不知道在哪里可以找到它们,在覆盖的函数中,我可以记录分配的内存块的大小和位置,而无需堆栈跟踪。

所以要处理它,我想我也需要记录堆栈跟踪(但是如何?),或者有什么方法可以找到导致内存泄漏的代码?

非常感谢您的帮助。

【问题讨论】:

标签: c++ windows memory-leaks stack-trace


【解决方案1】:

我使用以下方法向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__)

【讨论】:

  • 不过,这对第三方库没有任何作用。
【解决方案2】:

还有一些 COTS 可以为您显示内存泄漏,例如 Rational Purify (http://www-01.ibm.com/software/awdtools/purify/win/)。我提到这一点,因为我们在上一个职位上使用了它。

我想也有免费的。如有请注明。

【讨论】:

    猜你喜欢
    • 2021-09-01
    • 2011-02-18
    • 2012-07-16
    • 2010-09-08
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多