3

Is there a way to halt execution, somewhat like SIG_ABRT, but immediately, even if we're not on the main thread?

andyvn22
  • 14,696
  • 1
  • 52
  • 74

3 Answers3

3

Core Foundation has a macro HALT that should do the trick, if you really, really think it's necessary to do this:

#if defined(__ppc__)
    #define HALT do {asm __volatile__("trap"); kill(getpid(), 9); } while (0)
#elif defined(__i386__) || defined(__x86_64__)
    #if defined(__GNUC__)
        #define HALT do {asm __volatile__("int3"); kill(getpid(), 9); } while (0)
    #elif defined(_MSC_VER)
        #define HALT do { DebugBreak(); abort(); } while (0)
    #else
        #error Compiler not supported
    #endif
#endif
#if defined(__arm__)
    #define HALT do {asm __volatile__("bkpt 0xCF"); kill(getpid(), 9); } while (0)
#endif
jscs
  • 63,694
  • 13
  • 151
  • 195
  • These are nice, but have some problems. The asm instructions are essentially breakpoint generators, which is great if you expect to have a debugger attached, but maybe not what you want otherwise (ie: on windows it will trigger a crash/debug dialog by default). Oh, the MSC one calling abort() is ok, but still has more infrastructure/cruft than TerminateProcess() – Joe Mar 27 '12 at 23:07
  • @Joe: I don't think doing this in any fashion is a good idea at all. CF only uses this macro for fatal errors. The right answer to the question, as far as I'm concerned is "Yes, but you shouldn't". Note that I didn't write these, and that they are in active use. – jscs Mar 27 '12 at 23:10
  • That does the trick nicely! Thank you! @Joe: breakpoint generators is *perfect* for my case. – andyvn22 Mar 27 '12 at 23:18
1

If you're using a Macintosh app, "[[NSApp sharedApplication] terminate: self]" will work.

On an iPhone, you can even do "exit(-1)", but Apple will NOT accept any apps that abruptly or in any way terminate other than the user killing the app themselves. Here is a related question with some useful answers for you.

Community
  • 1
  • 1
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
0

Assuming you don't like exit() or _exit() which have some cleanup semantics, you can go with s

#include <signal.h>

kill( getpid(), SIGKILL );

And if you are on windows (assuming not since 'cocoa' tagged), you can use

TerminateProcess( GetCurrentProcess(), -1 );

This is all pretty harsh, so things should be pretty bad to do this.

Joe
  • 2,946
  • 18
  • 17