gpt4 book ai didi

Android NDK C++ 异常抛出 SIGSEGV 和 __gnu_cxx::__verbose_terminate_handler

转载 作者:太空狗 更新时间:2023-10-29 12:41:26 24 4
gpt4 key购买 nike

我想在我的 JNI 代码中抛出一个 std::exception 的子类,该代码是使用 swig 包装的,但它并不真正相关,因为生成的代码相当简单:

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;
}

我已经添加了 -fexception 到标志,但是当用户代码抛出异常时 vm 仍然中止,catch block 中的代码没有被执行。

我测试了不同的 cpp 实现,gnuSTL_static 引发了 __gnu_cxx::__verbose_terminate_handler,看起来异常没有被处理,STLport_static 调用 abort()

标志:

Android.mk 中:LOCAL_CPP_FEATURES += 异常

Application.mk中:APP_CPPFLAGS += -fexceptionsAPP_CFLAGS += -fexceptions

我还尝试过强制重建 STLport 和 libcxx,并使用 gcc-4.6gcc-4.8clang

更新

即使是这个简单的代码也会中止

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

如果我用 std::set_terminate 设置一个终止函数,我的函数就会被调用,它显然没有被捕获

更新

没有“new”的简单代码确实有效,所以我怀疑我的异常有问题:

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;
};

我使用:抛出 MyNativeException("消息")

更新好的,这个异常定义确实有效:

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

前面哪里出了问题?

最佳答案

您的问题与将 C++ 与 Java 混淆有关。

抛出新的 std::exception();

上面那行代码在 Java 中可能一切都很好,但在 C++ 中就不同了。

  1. 您正在从自由存储区分配内存以创建一个对象。

  2. 但最重要的是,C++ 中的 new 返回一个指向动态分配内存的指针,您必须使用 delete 释放该内存。它与 Java 中的 new 不同。

因此 C++ 中的那行代码抛出的是指针值,而不是对象。不幸的是,您没有检测到这个问题,因为在 C++ 中您几乎可以抛出任何东西——std::exception 对象、int、指针值等。

如果您有一个捕获指针值的 catch block ,那么您会看到这一点。

例如:

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

但要轻松纠正您的尝试:

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

为了确认,这是一个小而完整的程序,它抛出异常并捕获它:

#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();
}
}

输出:abc123

我上了你的课,实际上抛出了你声称没有被捕获的异常。上面代码中的异常确实被捕获了,因此你的问题的唯一结论是你要么

  1. 没有抛出您声称正在抛出的异常,或者

  2. 你抛错了它(正如 new 的问题所指出的那样),或者

  3. 您的编译器有问题,或者您需要设置一个开关来启用异常处理。

关于Android NDK C++ 异常抛出 SIGSEGV 和 __gnu_cxx::__verbose_terminate_handler,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25130192/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com