Thread overview
D exit() function?
Mar 25, 2007
Sean Kelly
Mar 25, 2007
Saaa
Mar 25, 2007
Daniel Keep
March 25, 2007
I'm kind of dumbfounded.  I can't find any kind of D-compatible exit() function in phobos.  Using std.c.stdlib.exit will simply end the program, and module dtors/gc_term won't be called.  There's nothing in std.thread either.

Am I missing something?  Or is this something that should probably be added to phobos?


March 25, 2007
Jarrett Billingsley wrote:
> I'm kind of dumbfounded.  I can't find any kind of D-compatible exit() function in phobos.  Using std.c.stdlib.exit will simply end the program, and module dtors/gc_term won't be called.  There's nothing in std.thread either.
> 
> Am I missing something?  Or is this something that should probably be added to phobos? 

It doesn't currently exist.  However, "throw new Object" would accomplish much the same thing.


Sean
March 25, 2007
"Sean Kelly" <sean@f4.ca> wrote in message news:eu6fvd$f5r$2@digitalmars.com...

> It doesn't currently exist.  However, "throw new Object" would accomplish much the same thing.
>
>
> Sean

Except that it would print an error message, and exiting without an error is certainly a possible scenario.


March 25, 2007
>
>> It doesn't currently exist.  However, "throw new Object" would accomplish much the same thing.
>>
>>
>> Sean
>
> Except that it would print an error message, and exiting without an error is certainly a possible scenario.

Haha, so I wasn't just too dumb to find it :)


March 25, 2007
Jarrett Billingsley wrote:

> I'm kind of dumbfounded.  I can't find any kind of D-compatible exit() function in phobos.  Using std.c.stdlib.exit will simply end the program, and module dtors/gc_term won't be called.  There's nothing in std.thread either.
> 
> Am I missing something?  Or is this something that should probably be added to phobos?

I think simple scope(exit) should do the trick.

void exitFun(){
 ...
}

void main(){
   scope(exit) exitFun();
   ...
}

March 25, 2007
"Krzysztof Szukielojc" <krzysztof.szukielojc@gmail.com> wrote in message news:eu6m78$m1r$1@digitalmars.com...

>
> I think simple scope(exit) should do the trick.
>
> void exitFun(){
> ...
> }
>
> void main(){
>   scope(exit) exitFun();
>   ...
> }
>

No, I'm not looking for an atexit() replacement, sorry.  I'm looking for a way to end my D program at an arbitrarily deep call depth and still have it call the RTL cleanup functions.

I suppose it would be possible to do something like:

extern(C) void _cleanupD()
{
    _moduleDtor();
    gc_term();
}

..

void main()
{
    atexit(&_cleanupD);

    // program
}

But that's horribly non-portable.

Something like this should be in Phobos.


March 25, 2007

Jarrett Billingsley wrote:
> "Krzysztof Szukielojc" <krzysztof.szukielojc@gmail.com> wrote in message news:eu6m78$m1r$1@digitalmars.com...
> 
>> I think simple scope(exit) should do the trick.
>>
>> void exitFun(){
>> ...
>> }
>>
>> void main(){
>>   scope(exit) exitFun();
>>   ...
>> }
>>
> 
> No, I'm not looking for an atexit() replacement, sorry.  I'm looking for a way to end my D program at an arbitrarily deep call depth and still have it call the RTL cleanup functions.
> 
> I suppose it would be possible to do something like:
> 
> extern(C) void _cleanupD()
> {
>     _moduleDtor();
>     gc_term();
> }
> 
> ...
> 
> void main()
> {
>     atexit(&_cleanupD);
> 
>     // program
> }
> 
> But that's horribly non-portable.
> 
> Something like this should be in Phobos.

Another way of doing it, stolen from Python :P

> class SystemExit : Object {}
>
> int main(char[][] args)
> {
>     int result = 0;
>     try
>     {
>         result = appmain(args);
>     }
>     catch( SystemExit e )
>     {
>         // Meh.
>     }
>     return result;
> }
>
> int appmain(char[][] args)
> {
>     // Your code here!
> }
>
> // ...
>
> void really_deep_function()
> {
>     throw new SystemExit;
> }

This should be portable, and ensures that all dtors (class and module)
and scope(exit)/scope(failure) get executed.  It might take a little
longer to get back up to main, but at least it should be safe :)

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/