next up previous contents
Next: Consider Manual Deallocation as Up: Making the Most of Previous: Making the Most of

Don't Do Work in Destructors!

You absolutely, positively may not assume anything about the order in which objects are collected by Giggle (or by any garbage collection system).

Explain why assumptions about destruction order are impossible with any garbage collection system. Some assumptions are possible with some garbage collection algorithms, but in the general case, no assumptions are possible at all.

So use destructors only for freeing dynamically allocated memory that is not under the control of a garbage collection system, uninitializing variables by assigning special values to them, & maybe closing files. Files, though, are well-known examples of objects that require shutdown processing that should not be done in destructors, so your object should have closed files inside its own close or shutdown method.



Gene Michael Stover
2002-04-28