Android OS NDK C ++ excludes SIGSEGV and __gnu_cxx :: __ verbose_terminate_handler

I want to add a subclass std::exception

to my code JNI

, the code is wrapped with swig, but this is not very relevant as the generated code is pretty simple:

void function_that_throws_exception() {
    ...
    throw MyNativeException("Error");
}

try {
    function_that_throws_exception();
} catch(MyNativeException &_e) {
    jclass excep = jenv->FindClass("x/y/zMyNativeException");
    if (excep)
        jenv->ThrowNew(excep, (&_e)->what());
    return 0;
}

      

I added a flag -fexception

, but the vm still breaks when user code throws an exception, the code in the block is catch

not executed.

I tested various cpp implementations, gnustl_static

reises __gnu_cxx::__verbose_terminate_handler

it looks like no exception is being handled, stlport_static

callsabort()

flags:

in Android.mk

: LOCAL_CPP_FEATURES += exceptions

in Application.mk

: APP_CPPFLAGS += -fexceptions

APP_CFLAGS += -fexceptions

I also tried rebuilding stlport and libcxx command and using gcc-4.6

, gcc-4.8

andclang

UPDATE

even this simple code breaks

try
{
    throw new std::exception();
} catch (std::exception &e) {
}

      

and if I set a completion function with std::set_terminate

, my function is called, it is not explicitly caught

UPDATE

simple code without the "new" works, so I suspect there is something wrong with my exception:

class MyNativeException : public std::exception
{
public:
    explicit MyNativeException(const char *msg) : message(msg) {}
    virtual ~MyNativeException() throw() {}
    virtual const char *what() const throw() { return message.c_str(); }
protected:
    const std::string message;
};

      

I am throwing it using: throw MyNativeException("message")

UPDATE ok this exception definition works:

class MyNativeException
{
public:
    MyNativeException(const char *msg) : message(msg) {}
    ~MyNativeException() {}
    const char *what() { return message.c_str(); }
private:
    const std::string message;
};

      

what's wrong with the previous one?

+3


source to share


1 answer


Your problem is about obfuscating C ++ with Java.

throw new std::exception();

This line above might be fine and fine in Java, but that's a different story in C ++.

  • You are allocating memory from free storage to create an object.

  • But most importantly, new

    in C ++ it returns a pointer that points to dynamically allocated memory that you should free using delete

    . This is not the same as new

    Java.

So a line of code in C ++ is casting a pointer value, not an object. Unfortunately for you, you didn't find this problem, as in C ++ you can cast pretty much anything - object std::exception

, int, pointer value, etc.

If you have a catch block that caught a pointer value, you will see this.

For example:

try
{
   throw new std::exception();
} 
catch (std::exception *e) 
{
   delete e;
}

      



But it's easy to fix your attempt:

try
{
   throw std::exception();
} 
catch (std::exception &e) 
{
}

      

To confirm, here's a small but complete program that throws an exception and catches it:

#include <exception>
#include <string>
#include <iostream>

class MyNativeException : public std::exception
{
    public:
         explicit MyNativeException(const char *msg) : message(msg) {}
         virtual ~MyNativeException() throw() {}
         virtual const char *what() const throw() { return message.c_str(); }
    protected:
         const std::string message;
};

int main()
{
    try
    {
        throw MyNativeException("abc123");
    }
    catch (std::exception& e)
    {
        std::cout << e.what();
    }
}

      

Output: abc123

I took your class and actually threw the exception you claim is not being caught. The exception in the above code gets caught, so the only conclusion to your problem is that you either

  • The exception you claim is not thrown, or

  • You are throwing incorrectly (as mentioned in question c new

    ) or

  • Your compiler is broken, or there is a switch that needs to be set to enable exception handling.

+3


source







All Articles