views:

54

answers:

3

Does anyone know how I can for the g++ or gcc runtime to scramble the ram where an object was after delete? I have a theory that I'm using an object after it has been deleted but in practice it rarely crashes.

A: 

You can overload delete for your object if you like.

Paul Nathan
Is there a more global solution?
Steven Behnke
inline void operator delete(void* memblock) { //you custom stuff } would override the global. The different methods for testing for memory leaks or even for security follow this methodology. From the security standpoint, they zero out that region of memory before freeing it.
Digicoder
@Digicoder - I'd suggest posting this as its own answer.
Douglas Leeder
@Douglas - Will do, it at least makes it easier to read.
Digicoder
+3  A: 

I'd suggesting running with valgrind - that'll tell you if you're accessing memory after freeing it.

Douglas Leeder
Valgrind causes my game to crash.
Steven Behnke
Well - that might be due to the memory corruption you are trying to trace. Have you fixed all errors it reports before crashing?
Douglas Leeder
+1  A: 

inline void operator delete(void* memblock) { //you custom stuff } would override the global. I used to use this for security so that we could zero out the memory so its less likely to leak important information.

Digicoder