A bug in my code

bearophile bearophileHUGS at lycos.com
Mon Sep 8 14:05:03 PDT 2008


Sergey Gromov:

>I think that [...] is dangerous, not to mention redundant.  If there are references to elements in data other than in Blocks, reallocing data would silently invalidate those references eventually causing GPF (segfault).  The correct code is<

- It's a depressing, in such kind of code it seems I'm unable to write 5 lines of code without putting in dangerous bugs :-]
- That particular stack implementation is designed for ints only, so they don't contain references, so that code works (It seems).
- But you are right, if the data block contains references then the realloc(... ,0) are very bad.
- So generally in such programs the right thing to do seems to not dealloc memory, but remove the known references to it, and then let the GC deallocate such memory when it can't find references pointing to that memory.
- Yet, I have seen that for some data structures (probably ones with long chains of references) letting the GC do all the work of looking for unconnected components to deallocate them leads to long destruction times (2-10 seconds of time in one case). In such situations I think it may be better to give the GC a hand, actually deallocating the data structure inside the destructor using an efficient algorithm (if the programmer is sure it doesn't contain references to other allocate data) (this needs just few hundreds of seconds in the same case of before).

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list