Archived

This topic is now archived and is closed to further replies.

new & delete overload

This topic is 4942 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, i use overloading the new and delete operators to detect memory leaks like this:
#ifdef _DEBUG
      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

#ifdef _DEBUG
	#define GSnew new(__FILE__, __LINE__)
	#define GSdelete delete
#else
	#define GSnew new
	#define GSdelete delete
#endif
Each time i use the GSnew operator i get the following warning: warning C4291: 'void *__cdecl operator new(unsigned int,const char *,int)' : no matching operator delete found; memory will not be freed if initialization throws an exception thought had it overloaded thx in advance [edited by - freaker13 on May 31, 2004 3:54:20 PM]

Share this post


Link to post
Share on other sites
Read this. You might want to edit that operator new to follow the standard convention like this:

void* operator new(size_t size)
{
if (size == 0)
size = 1;

void* ptr;

while (!(ptr = malloc(size)))
{
new_handler h = std::set_new_handler(0);
set_new_handler(h);

if (h) h();
else throw bad_alloc();
}

return ptr;
}

Share this post


Link to post
Share on other sites