我尝试遵循这篇文章:http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml
重载我的新函数和删除函数以跟踪内存泄漏。
但是 - 如果我尝试编译,我会得到一个
C2365:“operator new”:重新定义;以前的定义是“函数”
在 xdebug 文件中
xdebug 包含在 xlocale 中 - 但是,我找不到我的项目包含 xlocale 的位置
我在项目中使用 MFC 进行多线程处理。
有人可以告诉我如何让内存泄漏跟踪发挥作用吗?
//编辑:
这是我的 findMemoryLeak.h,我将其包含在 stdafx.h 的末尾
#ifndef _FINDMEMORYLEAK_H
#define _FINDMEMORYLEAK_H
#include <list>
using namespace std;
#ifdef _DEBUG
typedef struct {
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;
typedef list<ALLOC_INFO*> AllocList;
AllocList *allocList;
void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
ALLOC_INFO *info;
if(!allocList) {
allocList = new(AllocList);
}
info = new(ALLOC_INFO);
info->address = addr;
strncpy(info->file, fname, 63);
info->line = lnum;
info->size = asize;
allocList->insert(allocList->begin(), info);
};
void RemoveTrack(DWORD addr)
{
AllocList::iterator i;
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
};
void DumpUnfreed()
{
AllocList::iterator i;
DWORD totalSize = 0;
char buf[1024];
if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++) {
sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString(buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString(buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString(buf);
};
inline void * __cdecl operator new(unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};
inline void __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
inline void * __cdecl operator new[](unsigned int size, const char *file, int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};
inline void __cdecl operator delete[](void *p)
{
RemoveTrack((DWORD)p);
free(p);
};
#endif
//make the normal new function call the new function with three parameters
#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#else
#define DEBUG_NEW new
#endif
#define new DEBUG_NEW
#endif
当我像这样将它包含在 stdafx.h 的末尾时,我收到数千个编译器错误,其中大多数在 xdebug 或 xlocale 中,第一个是
C2365:“operator new”:重新定义;以前的定义是“函数”
在 xdebug 中第 32 行