overloading new and delete

寵の児 提交于 2019-12-09 21:25:06

问题


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


回答1:


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.




回答2:


to find where xlocale is getting included. just changethe name of xlocale to something else. try to compile and you will see where it fails




回答3:


In Visual Studio, debug builds of programs already use a 'debug heap', so your own instrumentation is unnecessary.

Using the debug features of your platform, you could for example call _CrtDumpMemoryLeaks at the end of your program, without actually overloading everything.




回答4:


You are defining the very same overload that Microsoft defines for their own debugging, and running into theirs. I recommend adding an extra dummy parameter to your operator.

I also recommend debugging this not in stdafx.h before putting it there.




回答5:


Trying to debug this way with std lib is not really going to work out. It won't catch all (or any) of the memory allocation. It's just one of a million reasons not to use std lib, stl, or boost.



来源:https://stackoverflow.com/questions/1855406/overloading-new-and-delete

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!