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

Jacob Carlborg doob at me.com
Thu Dec 29 11:09:11 PST 2011


On 2011-12-29 18:22, Jakob Ovrum wrote:
> 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.

Could druntime hook up on the atexit function to run destructors and 
similar when the program exits?

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list