2

I've written a simple c++ test program to test exceptions

#include <iostream>
#include <string>

inline bool isNumber(char c);

inline unsigned stringToNum(const char *str) {
    unsigned x = 0;
    const char *p = str;
    while(1) {
        if( !isNumber(*p) ) {  
            if( *p == 0 && p != str )
                break;
            throw 0;
        }
        x = x * 10 + *p++ - '0';
    }
    return x;
}

inline bool isNumber( char c ) {
    return c >= '0' && c <= '9';
}

int main() {
    std::string k = std::string("test");
    try {
        int p = stringToNum(k.c_str());
    } catch(...) {
        std::cout << "EXCEPTION THROWN";
    }
}

I'm compiling the program in two stages (I'm attempting to simulate an error occuring with a much larger library). The first step looks like

/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/bin/i686-android-linux-g++ -c -fexceptions -frtti -Wnon-virtual-dtor -Woverloaded-virtual --sysroot "/Users/me/android-ndk-r8/platforms/android-14/arch-x86/"  -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -O3 -Wl,-rpath-link=/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -L/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -nostdlib /Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/crtbegin_dynamic.o -lc -lm -I/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/include -I/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/libs/x86/include exceptiontest.cpp

And then:

/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/bin/i686-android-linux-g++ -fexceptions -fPIC -pthread -rdynamic --sysroot "/Users/me/android-ndk-r8/platforms/android-14/arch-x86/" -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -O3 -Wl,-rpath-link=/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -L/Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/ -nostdlib /Users/me/android-ndk-r8/platforms/android-14/arch-x86/usr/lib/crtbegin_dynamic.o -lc -lm exceptiontest.o -L/Users/me/android-ndk-r8/toolchains/x86-4.4.3/prebuilt/darwin-x86/lib/gcc/i686-android-linux/4.4.3 -L/Users/me/android-ndk-r8/sources/cxx-stl/gnu-libstdc++/libs/x86 -lgnustl_shared -lgcc

When I run a.out on the device (with libgnustl_shared.so copied over to the libs directory), I get a segfault. What am I missing? compiler flag? library? etc.

edit: heres a stacktrace of the segfault off gdb

#0  get_cie_encoding (cie=0x6107daff) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:267
#1  0x080b16d8 in classify_object_over_fdes (ob=0x80d8b68, this_fde=0x80d0a1c) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:615
#2  0x080b1a5d in search_object (ob=0x80d8b68, pc=<value optimized out>) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:726
#3  0x080b1f84 in _Unwind_Find_FDE (pc=0x80b10da, bases=0xbfa61204) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2-fde.c:989
#4  0x080b07db in uw_frame_state_for (context=<value optimized out>, fs=0xbfa6102c) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2.c:1128
#5  0x080b0af8 in uw_init_context_1 (context=0xbfa61230, outer_cfa=<value optimized out>, outer_ra=0x809aa14) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind-dw2.c:1447
#6  0x080b10db in _Unwind_RaiseException (exc=0x944d060) at /Users/andrewhsieh/ndk-andrewhsieh/src.1-with-cherrypicks//build/../gcc/gcc-4.4.3/libgcc/../gcc/unwind.inc:88
#7  0x0809aa14 in __cxa_throw ()
#8  0x0804b4a7 in main () at exceptiontest.cpp:13
MEURSAULT
  • 8,307
  • 4
  • 24
  • 23
  • Segmentation faults are caused when **invalid** memory location are accessed. Look at the thread for much easier way of conversion.[std::string to int](http://stackoverflow.com/questions/200090/how-do-you-convert-a-c-string-to-an-int) – Mahesh Jun 18 '12 at 21:16
  • I know this is not the cause of the fault (it runs perfectly when compiled for the host platform). This has something to do with how the android ndk is set up with respect to exceptions – MEURSAULT Jun 18 '12 at 21:20

1 Answers1

0

Correct answer: Android NDK R5 and support of C++ exception

Incorrect old answer(this is no longer true.Thanks to Chris for pointing it out):

Exceptions in C++ are not supported on the Android platform. This is clearly stated in the Bionic documentation. Further, you can read this thread on the android-ndk google group for a detailed discussion on this topic.

Community
  • 1
  • 1
Samveen
  • 3,482
  • 35
  • 52
  • 1
    This is outdated. Exception support was added with the ndk-r5b see http://stackoverflow.com/questions/4663291/android-ndk-r5-and-support-of-c-exception which mentions an option flag that may be important. – Chris Stratton Jun 19 '12 at 17:00