www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - stdlib.exit()

reply David <d dav1d.de> writes:
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.
Aug 19 2012
next sibling parent reply "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
That's what I do:

import std.c.stdlib;

void main(string[] args)
{
    if( args.length == 1 )
    {
       writeln("Some arguments, please!");
       exit(-1);
    }
}
Aug 19 2012
parent "Minas Mina" <minas_mina1990 hotmail.co.uk> writes:
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.
Aug 19 2012
prev sibling parent reply Tracey <tracey.freitas gmail.com> writes:
I use the following:

import std.c.stdlib;
exit(0);
Aug 20 2012
parent reply David <d dav1d.de> writes:
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?
Aug 20 2012
next sibling parent reply "Minas" <minas_mina1990 hotmail.co.uk> writes:
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.
Aug 20 2012
parent David <d dav1d.de> writes:
 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.
Aug 20 2012
prev sibling parent reply "Regan Heath" <regan netmail.co.nz> writes:
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/
Aug 20 2012
next sibling parent reply "bearophile" <bearophileHUGS lycos.com> writes:
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
Aug 20 2012
parent reply Jacob Carlborg <doob me.com> writes:
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
Aug 20 2012
parent reply David <d dav1d.de> writes:
 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
If i interpret it correctly, a call to Runtime.terminate() (as I am doing right now), is the only and correct way?
Aug 20 2012
parent Jacob Carlborg <doob me.com> writes:
On 2012-08-20 22:04, David wrote:

 If i interpret it correctly, a call to Runtime.terminate() (as I am
 doing right now), is the only and correct way?
I would think so. -- /Jacob Carlborg
Aug 20 2012
prev sibling parent reply David <d dav1d.de> writes:
 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.
Thi is really not ideal, since this is for a argument-parser, --help should print the help and then exit.
Aug 20 2012
parent "Regan Heath" <regan netmail.co.nz> writes:
On Mon, 20 Aug 2012 21:03:25 +0100, David <d dav1d.de> wrote:

 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.
Thi is really not ideal
Didn't I just say that.. :p "Not ideal, but it would work." - Regan Heath
 , since this is for a argument-parser, --help should print the help and  
 then exit.
Still, it can be done.. int main(string[] args) { int retval = 0; try { ParseArguments(args); } catch(ExitException e) { // Silent retval = e.retval; } catch(Exception e) { writefln("Error: %s", e...); retval = 1; } return retval; } void ParseArguments(string[] args) { .. parse .. calls CmdHelp(); } void CmdHelp() { writeln(...); throw ExitException(0); } R -- Using Opera's revolutionary email client: http://www.opera.com/mail/
Aug 21 2012