2

HI All,

I was trying to overload new and delete to fix a memory leak problem in my project. But got stuck with some compilation error.

Currently this code is bit shabby

Here is my hdr file

#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);
  }

And my main.cpp is

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

char *ptr = new char;
DumpUnfreed();

return 0;
}

When i try to compile i get the following error

[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*)

I know there is some thing wrong with my #defines, but I am not sure what is wrong.

Can any one please bale me out of this

punith
  • 1,249
  • 1
  • 15
  • 18
  • 2
    Emmm... What if you change `unsigned int` to `size_t`? – sharptooth Aug 18 '10 at 05:35
  • ... `operator new( unsigned int ... )` compiles fine on Comeau though. `cstddef` is definitely worth taking a look at for the OP. – dirkgently Aug 18 '10 at 05:46
  • 1
    Maybe using `valgrind` (or similar tools) is the simpler way to fix your memory leak. – IanH Aug 18 '10 at 09:05
  • its not a leak that valgrind can detect. its a logical error like copy constructor or copy assignment called twice and it is assigning memory to already assigned pointer. – punith Aug 18 '10 at 09:11

2 Answers2

7

You've defined your new macro before your functions. Your code ends up looking like:

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

Which is obviously wrong. Your should move the macro definitions underneath the functions (or better is to keep those functions in a .cpp file you link with.) For what it's worth, new is a keyword and cannot be an identifier, so your program is, strictly speaking, ill-formed.

I recently posted my global memory operators framework. It might help you a bit.

Community
  • 1
  • 1
GManNickG
  • 494,350
  • 52
  • 494
  • 543
0

the signature don't match it sould be void* operator new (size_t size).

overriding single object new signature is static void * operator new(site_t size),

roni

roni bar yanai
  • 1,492
  • 10
  • 12