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

Ashish Myles marcianx at gmail.com
Thu Dec 29 09:59:56 PST 2011


On Thu, Dec 29, 2011 at 12:22 PM, Jakob Ovrum <jakobovrum at gmail.com> 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.

In that case, what is the recommended way to exit a program from a
deeply nested level and also possibly specify a return value?

Ashish


More information about the Digitalmars-d-learn mailing list