views:

479

answers:

5

I am looking for a simple and uncatchable way to terminate the Mac port of my C++ application. In Windows I was using

TerminateProcess(GetCurrentProcess, 0);

What's the equivalent command I can use with Mac OS X / XCode / GCC?

+1  A: 

exit(0);


exit is more like ExitProcess which tries to shutdown the application cleanly, but it might fail. TerminateProces is unconditional and can't be trapped.
Ismael
+6  A: 

Actually you want _exit if you want to have the same semantics as TerminateProcess. exit semantics are more closely aligned with ExitProcess.

Matt Davison
+1  A: 

Keep in mind that if either you call exit() or TerminateProcess(), you'll get you application terminated immediately, i.e. no destructor calls, no cleanup you may expect to be done is done (of course OS cleans up everything it can).

n0rd
exit will call the functions registered with atexit, so it is not the same.
Ismael
+3  A: 

A closer to ProcessTerminate will be to send a SIGKILL with kill, both terminate the current process immediately and can't be trapped. This is the same as _exit

kill(getpid(), SIGKILL);
Ismael
Or, a bit easier: `raise(SIGKILL)`.
Peter Hosey
+1  A: 

Actually, both exit() and _exit() involve the CRT, which means various actions are still taken. (Not sure about atexit, I haven't checked)

TerminateProcess on Windows is on the OS level, so it sidesteps all of the CRT. If you want to do the same thing on the Mac, your best bet is getting your hands dirty with mach functions. In this case:

#include <mach/mach.h>

... // lots of your code here

task_terminate(mach_task_self());

That's about as uncatchable as you can get.