View mode: basic / threaded / horizontal-split · Log in · Help
August 19, 2012
stdlib.exit()
I have to "exit" the running program, in C I would call

#include <stdlib.h>
exit(0);

in Python:

sys.exit(0);


What's the correct way to do that in D?
My current implementation:

import core.runtime : Runtime;
import core.stdc.stdlib : exit;

Runtime.terminate();
exit(0);

But that doesn't seem correct.
August 19, 2012
Re: stdlib.exit()
That's what I do:

import std.c.stdlib;

void main(string[] args)
{
   if( args.length == 1 )
   {
      writeln("Some arguments, please!");
      exit(-1);
   }
}
August 19, 2012
Re: stdlib.exit()
On Sunday, 19 August 2012 at 23:48:01 UTC, Minas Mina wrote:
> That's what I do:
>
> import std.c.stdlib;
>
> void main(string[] args)
> {
>    if( args.length == 1 )
>    {
>       writeln("Some arguments, please!");
>       exit(-1);
>    }
> }

Sorry, forgot to import std.stdio.
August 20, 2012
Re: stdlib.exit()
I use the following:

import std.c.stdlib;
exit(0);
August 20, 2012
Re: stdlib.exit()
Am 20.08.2012 09:15, schrieb Tracey:
> I use the following:
>
> import std.c.stdlib;
> exit(0);
>

Yeah, but that doesn't terminate the runtime and will not call any 
dtors, right?
August 20, 2012
Re: stdlib.exit()
I don't know about the runtime, but destructors are not called 
(not sure about module destructors)

import std.stdio;
import std.c.stdlib;

void main()
{
	S s;
	
	exit(-1);
}

struct S
{
	~this()
	{
		writeln("destructor");
	}
}

This prints nothing, meaning that the destructor wasn't called.

But why do you want destructors to be called when you call 
exit()? You are exiting in an "abnormal" way anyway.
August 20, 2012
Re: stdlib.exit()
On Mon, 20 Aug 2012 10:03:56 +0100, David <d@dav1d.de> wrote:

> Am 20.08.2012 09:15, schrieb Tracey:
>> I use the following:
>>
>> import std.c.stdlib;
>> exit(0);
>>
>
> Yeah, but that doesn't terminate the runtime and will not call any  
> dtors, right?

std.c.stdlib.exit is the C runtime exit function which doesn't know  
anything about D's runtime, which is layered on top.  So, I would not  
expect any D runtime cleanup to occur.

You could define a custom ExitException and throw that, catching it at the  
top level and returning the error code stored inside it, from main().  Not  
ideal, but it would work.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/
August 20, 2012
Re: stdlib.exit()
Regan Heath:

> You could define a custom ExitException and throw that, 
> catching it at the top level and returning the error code 
> stored inside it, from main().  Not ideal, but it would work.

Seems OK.
Another solution is to add a dexit() in Phobos :-)

Bye,
bearophile
August 20, 2012
Re: stdlib.exit()
On 2012-08-20 16:27, bearophile wrote:
> Regan Heath:
>
>> You could define a custom ExitException and throw that, catching it at
>> the top level and returning the error code stored inside it, from
>> main().  Not ideal, but it would work.
>
> Seems OK.
> Another solution is to add a dexit() in Phobos :-)

Or the D runtime could just use the "atexit" function defined in 
stdlib.h. Just add a callback which terminates the D runtime.

http://pubs.opengroup.org/onlinepubs/009695299/functions/atexit.html

-- 
/Jacob Carlborg
August 20, 2012
Re: stdlib.exit()
> But why do you want destructors to be called when you call exit()? You
> are exiting in an "abnormal" way anyway.

exit(0) is a normal termination, so destructors should be called. I 
wanna be nice and deallocate the allocated memory and unload dynamic 
libraries.
« First   ‹ Prev
1 2
Top | Discussion index | About this forum | D home