Archived

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

v71

overloading new and delete

Recommended Posts

Hi guys, i need a little bit of help here is the problem i''m trying to overload new and delete to kkep track of my memory leaks , her is how i do you it first define the functions #ifdef _DEBUG inline void * __cdecl operator new(unsigned int size, const char*file, int line ) { void *ptr = (void *)malloc(size); // AddAllocInfo( ptr, size, file, line); return(ptr); }; inline void __cdecl operator delete(void *p) { // RemoveAllocInfo( p ); free(p); }; #endif then i do a littl bit of macro trickery like this #ifdef _DEBUG #define DEBUG_NEW new(__FILE__, __LINE__) #else #define DEBUG_NEW new #endif #define new DEBUG_NEW the problem is that the complier says in the first line of the new function that it expects a string constant someone knows why , sorry it may be sounding easy for someone but it is the first time i do this macro playing

Share this post


Link to post
Share on other sites
Assuming this on the PC with MSVC, it''s probably going to be easier to enable the C Runtime Library Debug (crtdbg) functionality which will do the same thing!!! (i.e. record the line number and file from any allocation).
I posted the method here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=71992

--
Simon O''''Connor
Creative Asylum Ltd
www.creative-asylum.com

Share this post


Link to post
Share on other sites
Yeah , i knew that, but the problem is that sometimes ( almost everytime ) i debug using opengl in a mfc the debbugger crashes
reporting an unhandled exception caused by an unknown instrction (!!!!!!!!!) , so i was thinking to write a '' Mynew'' and ''Mydelete'' function but changing them all over the files involved in the project is a pain in the ass!!!

Share this post


Link to post
Share on other sites
I used that exact code and it works on my test program. I''m not sure what the problem could be, I included stdlib.h as my only header file. I assume you''re using VC++, as the only thing I could see is __FILE__ not being defined properly.

My short program:

  
#include <stdlib.h>

#ifdef _DEBUG
inline void * __cdecl operator new(unsigned int size,
const char*file,
int line )
{
void *ptr = (void *)malloc(size);


// AddAllocInfo( ptr, size, file, line);

return(ptr);
}


inline void __cdecl operator delete(void *p)
{


// RemoveAllocInfo( p );

free(p);
}

#endif


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

#else
#define DEBUG_NEW new

#endif
#define new DEBUG_NEW



void main()
{
int* x = new int;

delete x;

}



Desert Fox,
Matt Stock

Share this post


Link to post
Share on other sites