重载新和删除 [英] overloading new and delete
问题描述
我尝试关注这篇文章:
http://flipcode.com/archives/How_To_Find_Memory_Leaks .shtml
重载我的新功能和删除功能,以便跟踪内存泄漏。
但是 - 如果我尝试编译,我得到一个
C2365:operator new:redefinition;上一个定义是文件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地址;
DWORD size;
char file [64];
DWORD行;
} 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) - & 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\\\
,
(* i) - > file,(* i) - >行, ) - >地址,(* i) - > size);
OutputDebugString(buf);
totalSize + =(* i) - > size;
}
sprintf(buf,------------------------------------ ----------------------- \\\
);
OutputDebugString(buf);
sprintf(buf,Total Unfreed:%d bytes\\\
,totalSize);
OutputDebugString(buf);
};
inline void * __cdecl operator new(unsigned int size,const char * file,int line)
{
void * ptr =(void *)malloc尺寸);
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 ;
AddTrack((DWORD)ptr,size,file,line);
return(ptr);
};
inline void __cdecl operator delete [](void * p)
{
RemoveTrack((DWORD)p);
free(p);
};
#endif
//使正常的新函数调用具有三个参数的新函数
#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:redefinition;之前的定义是第32行的xdebug中的函数
p>我解决了一段时间。
发生的是,当你到达你的重载的时候, new
它没有解决我们的链接问题),但尝试添加:
#undef new
在文件中最后一个include伪指令之后,但在第一个 new
>
编辑
这是因为 stdafx.h
else包括一个文件,定义DEBUG_NEW)包括你的内存泄漏检测代码在一些CPP文件(你应该能够弄清楚从编译器错误)。因此, new
已定义为一个宏,它会导致编译器在定义时发出barf。
I try to follow this article: http://flipcode.com/archives/How_To_Find_Memory_Leaks.shtml
to overload my new and delete functions in order to track memory leaks.
however - if I try to compile, I get a
C2365: "operator new": redefinition; previous definition was a "function"
in the file xdebug
xdebug gets included in xlocale - however, i can't find where my project is including xlocale
I'm using MFC for multithreading in my project.
Can someone tell me how I can get my memory leak tracking to work?
//edit: So this is my findMemoryLeak.h which i include in the end of 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
when I include it like this in the end of stdafx.h, i get thousands of compilererrors, most of them in either xdebug or xlocale, with the first being
C2365: "operator new": redefinition; previous definition was a "function"
in xdebug on line 32
I solved this a while ago.
What is happening is that the word new
is a macro by the time you get to your overloads (granted, it didn't solve our linking problems) but try adding:
#undef new
after the last include directive in your file but before the first new
overload.
edit
This happens because stdafx.h
(or something else includes a file that defines DEBUG_NEW) is included before you include your memory leak detection code in some CPP file (you should be able to figure out which from the compiler errors). Thus new
has been defined as a macro which causes your compiler to barf at the definition.
这篇关于重载新和删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!