I have seen all kinds of attempts, including this one:
void m_free(void **p) { if (*p != NULL) { free(*p); *p = NULL; } }
Deploying dereferencing like interrupt not only breaks down various platforms, โincluding this example inโ can only work if you initialize, free and reinitialize every single pointer in every existing function (compiled libraries are included) and work with C, which does the same thing .
Then apply optimization and blocking of safe flows. Bang!
In short, if you canโt keep track of what you have allocated in one function, it is time to change this function. Do it enough ... and you will find that the need for secure free () goes away quickly. Valgrind is your friend if you work on a supported platform. According to your tags, this is really your friend :)
Or use malloc (), which collects garbage at its own expense, depending on how you distribute things and get rid of free () at all. After that, debugging becomes almost infinitely interesting.
Hopefully you are rebuilt? Although you seem to have a problem (also) with mutual exclusion, it just leads to re-factorization. Ie, let the line before free () block or fail when trying to get a lock .. and set the free pointers to NULL in the thread with the lock, at least in the structures you implement.
source share