On Wed, 10 Apr 2013 05:48:30 -0400, Henning Pohl <henn...@still-hidden.de>
wrote:
Any instance of B always needs to be destructed _before_ the A it's
connected to. How do you express this in D?
You must not rely on the GC if you need ordered destruction.
Even though destructors CAN destroy other resources, it's almost always
better to use manual destruction. Consider that you almost certainly have
more memory available than limited resources (such as file handles). If
you rely on the GC to destroy that resource (and the GC is not guaranteed
to always destroy it), then it's possible you run out of file handles
before the memory that owns it is collected.
I recommend you introduce destruction methods to close the handles
manually outside the GC. Then if you happen to forget to close those
manually, as a fail-safe have A destroy its a_handle (which would destroy
all the b_handles, right?) in the destructor.
A simple example to explain this is a buffered stream. Imagine you have a
class which contains GC-managed array as a buffer, and the OS file handle.
Upon destruction, it would be best if the buffered stream wrote whatever
buffered data was left into the file handle, but since the GC may have
destroyed the buffer, we can't do that. So you have to put a warning in
the docs not to rely on the GC to destroy the stream.
It really requires a change in philosophy -- do not let the GC clean up
non-memory resources. Use manual methods to do that.
-Steve