5

I'm making my own assert macro using Android NDK in C++ like in this answer, but there is an std::exit function that can not be used in Android. Is there some alternative like std::exit in Android?

Community
  • 1
  • 1
Tom
  • 1,027
  • 2
  • 16
  • 35
  • 1
    C++ specifically says *Finally, control is returned to the host environment.* If a hosted implementation does not do this, it is non-conforming. – chris Jul 22 '14 at 19:24
  • I just changed the std::exit function to assert(0) and it works – Tom Jul 22 '14 at 20:33
  • 1
    Have you tried `std::terminate`? https://android.googlesource.com/platform/ndk.git/+/429798ff636b0553e4ff3c692067718caa9c454e/sources/cxx-stl/gabi++/src/terminate.cc – Dannie Jul 23 '14 at 03:10
  • @Dannie It works just like assert(0), but std::terminate seems to be better choice. If you like you can convert your comment to an answer, and I will accept it. Thank you. – Tom Jul 23 '14 at 10:16

1 Answers1

7

Had a similar requirement and found std::terminate works. In particular, on iOS (and quite likely Android) it causes a "forced" crash which Application Performance Management Tools (aka Crash Loggers) like Crashlytics would pick up and report.

Additional Reference:

Dannie
  • 2,430
  • 14
  • 16
  • 5
    Is there a way that it would not look like a crash ? Right now I see a message "Unfortunately has stopped". In my app, at some specific stage that requires a restart of the app, I want to just terminate the app as soon as it is paused. Like a forced jettison... – Mic Aug 11 '15 at 08:41
  • 1
    In some case, `std::terminate()` just crashes the caller thread instead of quitting the app. I finally have to resort to the C API `exit()`. – kakyo Jul 01 '20 at 07:59