digitalmars.D.learn - stdlib.exit()
- David (12/12) Aug 19 2012 I have to "exit" the running program, in C I would call
- Minas Mina (10/10) Aug 19 2012 That's what I do:
- Minas Mina (2/12) Aug 19 2012 Sorry, forgot to import std.stdio.
- Tracey (3/3) Aug 20 2012 I use the following:
- David (3/6) Aug 20 2012 Yeah, but that doesn't terminate the runtime and will not call any
- Minas (20/20) Aug 20 2012 I don't know about the runtime, but destructors are not called
- David (3/5) Aug 20 2012 exit(0) is a normal termination, so destructors should be called. I
- Regan Heath (10/18) Aug 20 2012 std.c.stdlib.exit is the C runtime exit function which doesn't know
- bearophile (5/8) Aug 20 2012 Seems OK.
- Jacob Carlborg (6/12) Aug 20 2012 Or the D runtime could just use the "atexit" function defined in
- David (2/5) Aug 20 2012 If i interpret it correctly, a call to Runtime.terminate() (as I am
- Jacob Carlborg (4/6) Aug 20 2012 I would think so.
- David (2/5) Aug 20 2012 Thi is really not ideal, since this is for a argument-parser, --help
- Regan Heath (36/42) Aug 21 2012 Didn't I just say that.. :p
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
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
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
I use the following: import std.c.stdlib; exit(0);
Aug 20 2012
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
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
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
On Mon, 20 Aug 2012 10:03:56 +0100, David <d dav1d.de> wrote:Am 20.08.2012 09:15, schrieb Tracey: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/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
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
On 2012-08-20 16:27, bearophile wrote:Regan Heath: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 CarlborgYou 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 :-)
Aug 20 2012
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.htmlIf i interpret it correctly, a call to Runtime.terminate() (as I am doing right now), is the only and correct way?
Aug 20 2012
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
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
On Mon, 20 Aug 2012 21:03:25 +0100, David <d dav1d.de> wrote:Didn't I just say that.. :p "Not ideal, but it would work." - Regan HeathYou 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.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