适用于Visual Studio 2010的C ++内存泄漏工具

时间:2012-06-21 12:23:52

标签: c++ visual-studio-2010 visual-studio

有人可以建议我使用Visual Studio 2010中集成的内存泄漏插件吗?任何建议都是好的。

3 个答案:

答案 0 :(得分:5)

您可以尝试使用Visual Leak Detector。它适用于Visual C ++。

答案 1 :(得分:1)

您可以尝试BoundsChecker,它可以与Visual Studio集成!

答案 2 :(得分:1)

您可以执行以下操作:

#ifdef _DEBUG
# define _CRTDBG_MAP_ALLOC 1 // better in project file in DEBUG mode
# include <crtdbg.h>
# include <new>
# define malloc(size)       _malloc_dbg(size,_CLIENT_BLOCK,__FILE__,__LINE__)
# define realloc(addr,size) _realloc_dbg(addr,size,_CLIENT_BLOCK,__FILE__,__LINE__)
# define free(addr)         _free_dbg(addr,_CLIENT_BLOCK)

void * __stdcall operator new ( size_t size, const char * filename, int linenumber )
{
  void * tmp = _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
  if ( tmp == NULL )
    throw std::bad_alloc;
  return tmp;
}
void * __stdcall operator new [] ( size_t size, const char * filename, int linenumber )
{
  void * tmp = _malloc_dbg( size, _CLIENT_BLOCK, filename, linenumber );
  if ( tmp == NULL )
    throw std::bad_alloc;
  return tmp;
}
// If you need, you could implement the nothrow version of new operator, too.
void __stdcall operator delete( void *p, const char * filename, int linenumber )
{
  _free_dbg(p,_CLIENT_BLOCK)
}
void __stdcall operator delete[]( void *p, const char * filename, int linenumber )
{
  _free_dbg(p,_CLIENT_BLOCK)
}
void __stdcall operator delete( void *p )
{
  _free_dbg(p,_CLIENT_BLOCK)
}
void __stdcall operator delete[]( void *p )
{
  _free_dbg(p,_CLIENT_BLOCK)
}
// if there is MFC, you could use MFC style DEBUG_NEW
# ifdef DEBUG_NEW
#  define new DEBUG_NEW
# else
#  define DEBUG_NEW_HEAP new( __FILE__, __LINE__ )
#  define new DEBUG_NEW_HEAP
# endif
#endif

通过这种方式,您不需要分析器或任何其他插件,Visual Studio会自动为您收集内存泄漏(而专业版就足够了)。