Skip to main content
GameDev.net gamedev.net
🔒 Locked

gcc: overloading operator delete error

Started by Erluk Jul 3, 2004 at 6:01 PM 1 replies 1k views
Original Post
Erluk
Erluk
Hi all! I'm currently trying to rewrite and integrate the memory tracker from TripleBuffer's DebugKit with my game. But when using the re-#defined operator delete, I get the build errors shown below. Google didn't really help here. Can somebody help me to solve these errors?
src/apps/demos/simplegame/simplegame.cpp: In function `int main(int, const 
   char* const*)':
src/apps/demos/simplegame/simplegame.cpp:52: `operator delete(void*)(intptr)' 
   has type `void' and is not a throw-expression
src/apps/demos/simplegame/simplegame.cpp:55: `((intarrayptr != 0) ? operator 
   delete [](void*)(intarrayptr) : 0)' has type `void' and is not a 
   throw-expression
I'm using MinGW + MSYS; gcc --version reports gcc.exe (GCC) 3.2.3 (mingw special 20030504-1). Here is some more code: relevant lines from simplegame.cpp
int* intptr = new int;
	delete intptr;

	int* intarrayptr = new int[15];
	delete [] intarrayptr;
the same lines as above after preprocessing:
int* intptr = (pgCore::pgMemoryAllocator::SetOwner("src/apps/demos/simplegame/simplegame.cpp",51,__FUNCTION__),false) ? __null : new int;
        (pgCore::pgMemoryAllocator::SetOwner("src/apps/demos/simplegame/simplegame.cpp",52,__FUNCTION__),false) ? __null : delete intptr;

        int* intarrayptr = (pgCore::pgMemoryAllocator::SetOwner("src/apps/demos/simplegame/simplegame.cpp",54,__FUNCTION__),false) ? __null : new int[15];
        (pgCore::pgMemoryAllocator::SetOwner("src/apps/demos/simplegame/simplegame.cpp",55,__FUNCTION__),false) ? __null : delete [] intarrayptr;
relevant part of memtracker.h
#ifdef PG_USE_MEMORY_TRACKER
inline void* operator new (size_t ReportedSize)
{
	return pgCore::pgMemoryAllocator::New (ReportedSize);
}

inline void* operator new[] (size_t ReportedSize)
{
	return pgCore::pgMemoryAllocator::ArrayNew (ReportedSize);
}

inline void* operator new (size_t ReportedSize, const char* SourceFile, const uint32 SourceLine)
{
	return pgCore::pgMemoryAllocator::New (ReportedSize, SourceFile, SourceLine);
}

inline void* operator new[] (size_t ReportedSize, const char* SourceFile, const uint32 SourceLine)
{
	return pgCore::pgMemoryAllocator::ArrayNew (ReportedSize, SourceFile, SourceLine);
}

inline void operator delete (void* ReportedAddress)
{
	return pgCore::pgMemoryAllocator::Delete (ReportedAddress);
}

inline void operator delete[] (void* ReportedAddress)
{
	return pgCore::pgMemoryAllocator::ArrayDelete (ReportedAddress);
}

#define PG_MT_NEW (pgCore::pgMemoryAllocator::SetOwner(__FILE__,__LINE__,__FUNCTION__),false) ? NULL : new
#define PG_MT_DELETE (pgCore::pgMemoryAllocator::SetOwner(__FILE__,__LINE__,__FUNCTION__),false) ? NULL : delete
#define PG_MT_MALLOC(sz) (pgCore::pgMemoryAllocator::Allocator(__FILE__,__LINE__,__FUNCTION__,pgCore::PG_MEMORY_ALLOCATION_TYPE_MALLOC, sz)
#define PG_MT_CALLOC(sz) (pgCore::pgMemoryAllocator::Allocator(__FILE__,__LINE__,__FUNCTION__,pgCore::PG_MEMORY_ALLOCATION_TYPE_CALLOC, sz)
#define PG_MT_REALLOC(ptr,sz) (pgCore::pgMemoryAllocator::Reallocator(__FILE__,__LINE__,__FUNCTION__,pgCore::PG_MEMORY_ALLOCATION_TYPE_REALLOC, sz, ptr)
#define PG_MT_FREE(ptr) (pgCore::pgMemoryAllocator::Free(__FILE__,__LINE__,__FUNCTION__,pgCore::PG_MEMORY_ALLOCATION_TYPE_FREE, ptr)

#else

#define PG_MT_NEW new
#define PG_MT_DELETE delete
#define PG_MT_MALLOC(sz) malloc(sz)
#define PG_MT_CALLOC(sz) calloc(sz)
#define PG_MT_REALLOC(ptr,sz) realloc(ptr,sz)
#define PG_MT_FREE(ptr) free(ptr)

#endif

#define new PG_MT_NEW
#define delete PG_MT_DELETE
#define malloc(sz) PG_MT_MALLOC(sz)
#define calloc(sz) PG_MT_CALLOC(sz)
#define realloc(ptr,sz) PG_MT_REALLOC(ptr,sz)
#define free(ptr) PG_MT_FREE(ptr)

#endif
Problems with Windows? Reboot! - Problems with Linux? Be root!
Fruny
Fruny
operator delete doesn't return a value, therefore you can't use it within :? like that.

Try rewriting your macros so that they expand to:

(pgCore::pgMemoryAllocator::SetOwner("blah",52,__FUNCTION__),false) ? __null : (delete intptr, __null);

and

(pgCore::pgMemoryAllocator::SetOwner("blah",55,__FUNCTION__),false) ? __null : (delete[] intptr, __null);


Or something similar.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Erluk
Erluk
Thanks for your help! I removed the operator ? : from my macros, and was finally able to compile my project.
Problems with Windows? Reboot! - Problems with Linux? Be root!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.