www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - dmain2.d - When should _moduleDtor() be called?

I was looking at /dmd/src/phobos/internal/dmain2.d today when I noticed 
something about when _moduleDtor() is called.  Currently, it's called 
immediately after calling the user-defined main function.  The problem I 
have with this is - what if an exception happens?  The module dtors are 
never called.  The spec for modules says that "Static destructors for 
individual modules will only be run if the corresponding static constructor 
successfully completed," but with the current code, if a module ctor throws 
an exception, _no_ module dtors are called, even if their corresponding 
ctors completed successfully!

(And I just noticed that gc_term() is also liable not to be called, though 
I'm not sure if it's supposed to be when an exception is thrown.)

As such, I propose that the call to _moduleDtor() be moved (in the 
exception-handling version anyway) to a finally block following the existing 
try-catch block.  Additionally, the WinMain in the D for Win32 spec page 
should be changed to match.  (interestingly, in the WinMain example, 
gc_term() is called _after_ the try-catch block, meaning it's always called, 
exception or not.. maybe this is a hint?) 
May 17 2006