• Advertisement
Sign in to follow this  

Problem with simple memory manager

This topic is 4248 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

Hello, i'm writing a simple memory manager...it's main intention is to track memory allocations/deallocations in debug mode to prevent memory leaks. However I experience some problems with it whitch I can't solve for some time. Generaly the idea is simple: overide the global new and delete operators with custom ones which use the memory manager class to allocate memory and track what gets allocated and deallocated...when the application terminates what is allocated but not deallocated is a memory leak. My new and delete operators are the following: inline void* __cdecl operator new(unsigned int size, const char* file, int line) { return MemoryManager::GetSingletonPtr()->AllocateMemory(size, file, line); } inline void __cdecl operator delete(void* ptr, const char* file, int line) { MemoryManager::GetSingletonPtr()->DeAllocateMemory(ptr); } Then I use the folowing defines to prevent writing file and line as an argument each time: #ifdef _DEBUG #define new new(__FILE__, __LINE__) #define delete delete(__FILE__, __LINE__) #endif Well...everything seems ok but actualy only the define for the new operator works. The one for delete generates bunch of errors like: error C2541: 'delete' : cannot delete objects that are not pointers error C2146: syntax error : missing ';' before identifier 'Thing that I'm trying to delete' The other thing I tried to do is remove the file and line arguments from the delete operator, things work then but I really can't find a way to remove the overide when I need to use the original delete operator (in the memory manager class for example) The other problem with this was lots of warnings about "no matching delete operator found" I would be very gratefull if you guys can help me with this!

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by ShaderDev
Well...everything seems ok but actualy only the define for the new operator works. The one for delete generates bunch of errors like:


That is because the delete operator can be overloaded for only void* or void*, size_t parameters (if I'm not wrong!). The new operator, however, doesn't suffer from such parameter restriction requiring that only the first parameter be an explicit size_t parameter.

Just replace the fancy delete overload with the normal version and remove the #define delete too. You should still be able to track memory allocations fairly well depending on how you wrote your system. ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement