kornalius wrote:I tried using IsBadWritePtr... and it doesn't work, it returns 0 if the memory block is assigned and 0 if it's freed also.
What would then be the best method if I create my own methods for malloc, calloc, free and strdup without losing speed?
I was thinking about assigning a 4 bytes value at the beginning of the block and then return the pointer as location + 4 in malloc and calloc. In free, I could reset the pointer - 4 (4 bytes value) to zero let's say and if I need to check to see if the pointer exists, I could check if pointer - 4 is zero or not?
Would this work even though the location as been freed?
I can tell you that not only have I used this before, but some very large video game companies have used this and shipped countless games with it. As far as 32 byte alignment goes, that should be a non-issue as your extra 4 byte padding would only be added in debug mode. Release mode would be raw allocs/frees.
That 4 byte header can be a pointer to an allocation table in debug mode to tell you not only if the memory has been freed, but where the memory was allocated, how much was allocated, what its allocation number was, etc. You get some really nice heapdumps with all that info, which is useful for systems with crappy memory managers.