2

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

4

3 回答 3

5

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

于 2012-06-21T12:29:50.863 回答
1

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

于 2012-06-21T12:39:04.203 回答
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 会自动为您收集内存泄漏(专业版就足够了)。

于 2012-06-21T13:39:50.417 回答