Bug or feature? std.c.stdlib.exit() breaks RAII

Jakob Ovrum jakobovrum at gmail.com
Thu Dec 29 09:22:32 PST 2011


On Thursday, 29 December 2011 at 16:27:33 UTC, Ashish Myles wrote:
> std.c.stdlib.exit() seems to break RAII. The code below tests 
> this
> both using a struct destructor and an explicit scope(exit) {}.  
> Is
> this an intentional feature or a bug?
>
> import std.stdio;
> import std.c.stdlib;
>
> void main()
> {
>   struct SafeExit {
>       ~this() {
>           writeln("Safely exit with destructor.");
>       }
>   }
>   SafeExit safeExit;
>
>   scope(exit) { writeln("Safely exit with scope(exit)."); }
>   scope(failure) { writeln("Safely exit with scope(failure)."); 
> }
>
>   writeln("Test if std.c.stdlib.exit() breaks RAII.");
>   writeln("Pre-exit!");
>   std.c.stdlib.exit(0);
>   writeln("Post-exit! Should not get here!");
> }

The C runtime is beyond D's immediate control. You would have to 
replace C's exit function with a custom one or make the compiler 
recognize calls to it.

Calling 'exit' doesn't properly shut down the D runtime either, 
it's not just constructors.

It's neither a bug or a feature. The bug is arguably in your 
program.


More information about the Digitalmars-d-learn mailing list