I’m reading Andrei Alexandrescu’s The D Programming Language and found this nugget in the tear-down sequence:
…D assumes the exiting the application will de facto free all resources associated with it, so it does not invoke any destructor.
This works great for memory resources, but what about things like network sockets, custom hardware, file handles, etc.? Is there a way to guarantee that my destructor is always called? Alternatively: Does D provide a better way of handling these things (and I’m stuck in a C++ mindset)?
You can use a static destructor which gets called on thread termination and the shared static destructor which gets called on (normal) application shutdown
(Now if only we had weak references so we wouldn’t need another level of indirection…)