【问题标题】:Try...catch causes segmentation fault on embedded ARM with posix threadsTry...catch 会导致具有 posix 线程的嵌入式 ARM 上出现分段错误
【发布时间】:2011-04-26 23:56:28
【问题描述】:

今天,我发布了一个关于 std::string 销毁后出现分段错误的问题(请参阅this post)。我已经剥离了代码,因此我不使用 STL,并且有时仍然会出现分段错误。

以下代码在我运行 Linux 的 PC 上运行良好。但是使用我们的嵌入式设备制造商提供的 ARM 交叉编译器,它会在 catch (...) 之前出现分段错误。

这个问题在Google Groups中好像有this post的链接,但是我还没有找到解决办法。

代码是使用 ARM 交叉编译器编译的

仍然欢迎任何建议!

#include <stdlib.h> #include <stdio.h> #include <pthread.h> #include <unistd.h> void *ExecuteThreadMethod(void *AThread); class Thread { private: pthread_t internalThread; public: void RunSigSegv() { try { for (int i = 0; i < 200; i++) { usleep(10000); } } // <---- Segmentation fault occurs here catch(...) { } } void Start() { pthread_attr_t _attr; pthread_attr_init(&_attr); pthread_attr_setdetachstate(&_attr, PTHREAD_CREATE_DETACHED); pthread_create (&internalThread, &_attr, ExecuteThreadMethod, this); } }; void *ExecuteThreadMethod(void *AThread) { ((Thread *)AThread)->RunSigSegv(); pthread_exit(NULL); } Thread _thread1; Thread _thread2; Thread _thread3; Thread _thread4; void s() { _thread1.Start(); _thread2.Start(); _thread3.Start(); _thread4.Start(); } int main(void) { s(); usleep(5000000); }

【问题讨论】:

    标签: linux multithreading gcc posix arm


    【解决方案1】:

    只是一个诊断问题:如果您不在 Start() 中分离线程会发生什么?你必须 pthread_join() 回到 main() 中的线程。

    另外,你考虑过Boost's threads吗?这可能更合适,因为您使用的是 C++ 而不是 C。

    【讨论】:

    • 不幸的是,我尝试了可连接线程,但没有任何区别。
    【解决方案2】:

    我曾经遇到过这样的问题,它是由于链接到不支持线程的libstdc++ 版本引起的,这意味着所有线程共享一个公共异常处理堆栈,从而导致灾难性后果。

    确保使用--enable-threads=posix 配置交叉编译器及其库。

    【讨论】:

    • 这段代码是在没有libstdc++的情况下编译的。它不应该使用任何 STL 例程(据我所知)。
    • @Laurens:与g++链接时会隐式包含;各种语言支持都需要它,包括异常处理以及标准库。
    猜你喜欢
    • 2012-02-14
    • 2017-05-26
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多