在c ++中重载new和delete

时间:2010-08-18 05:32:22

标签: c++

全部好,

我试图重载new并删除以修复项目中的内存泄漏问题。但是遇到了一些编译错误。

目前这段代码有点破旧

这是我的hdr文件

#include <cstddef>
#include <iostream>
#include <list>
#include <stdarg.h>
#include <stdio.h>
using namespace std;
typedef unsigned int  DWORD;
void AddTrack(DWORD addr,  DWORD asize,  const char *fname, DWORD lnum);
char *OutputDebugString (const char *fmt, ...);
void RemoveTrack(DWORD addr);
void DumpUnfreed();

#ifdef _DEBUG
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW


   void *  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 * operator new(unsigned int size)
  {
          void *ptr = (void *)malloc(size);
          AddTrack((DWORD)ptr, size, _FILE_,_LINE_);
          return(ptr);
  }*/
  void  operator delete(void *p)
  {
          RemoveTrack((DWORD)p);
          free(p);
  }

#endif


char *OutputDebugString (const char *fmt, ...)
{

char *p = NULL;
 size_t size = 1024;
 int n = 0;
 va_list ap;

 if((p = (char*) malloc(size)) == NULL)
 return NULL;

 while(1) {
  va_start(ap, fmt);
  n = vsnprintf(p, size, fmt, ap);
  va_end(ap);

 if(n > -1 && n < size)
return p;

/* failed: have to try again, alloc more mem. */
if(n > -1)      /* glibc 2.1 */
size = n + 1;
else            /* glibc 2.0 */
size *= 2;     /* twice the old size */

if((p = (char *)realloc (p, size)) == NULL)
return NULL;
}
}

typedef struct information {
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;
            allocList = (AllocList*)malloc (sizeof (AllocList));
          }

          //info = new(ALLOC_INFO);
          info = (ALLOC_INFO*) malloc (sizeof (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)
          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("%s",buf);
                  totalSize += (*i)->size;
          }
          sprintf(buf, "-----------------------------------------------------------\n");
          OutputDebugString("%s",buf);
          sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
          OutputDebugString("%s",buf);
  }

我的main.cpp是

#include "mynew.h"
int main()
{

char *ptr = new char;
DumpUnfreed();

return 0;
}

当我尝试编译时,我收到以下错误

[root@dhcppc0 new]# !g
g++ main.cpp -D_DEBUG
mynew.h:25: error: declaration of ‘operator new’ as non-function
main.cpp: In function ‘int main()’:
main.cpp:9: error: no matching function for call to ‘operator new(unsigned int, const     char [9], int)’
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:84: note:   candidates are: void* operator new(size_t)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:88: note:                     void* operator new(size_t, const std::nothrow_t&)
/usr/lib/gcc/i386-redhat-linux/4.1.1/../../../../include/c++/4.1.1/new:94: note:                    void* operator new(size_t, void*)

我知道我的#defines有些问题,但我不确定是什么问题。

任何人都可以把我从这个打包出来

2 个答案:

答案 0 :(得分:7)

您已在功能之前定义了new宏。您的代码最终看起来像:

void *
  operator new(__FILE__, __LINE__)(unsigned int size, const char *file, int line)

这显然是错误的。你应该在函数下面移动宏定义(或者更好的是将这些函数保存在你链接的.cpp文件中。)对于它的价值,new是一个关键字,不能是一个标识符,所以你的程序是严格来说,形成不良。

我最近发布了global memory operators framework。它可能对你有所帮助。

答案 1 :(得分:0)

签名不匹配应该是void* operator new (size_t size)

覆盖单个对象的新签名是 static void * operator new(site_t size)

罗尼