ARMv7: ライブラリで例外が発生すると、「アクティブな例外なしで呼び出しを終了します」

ARMv7: ライブラリで例外が発生すると、「アクティブな例外なしで呼び出しを終了します」

私はgcc 4.9.2を使ってarmv7hv(gcc-4.9.2_armv7hf_glibc-2.9)をクロスコンパイルしています。主な実行可能ファイルとエクスポートされた関数を含むライブラリがありますFoo()

私の経験では、そのライブラリの関数から例外をスローしてFoo()すぐにキャッチしようとするとキャッチされます。

しかし、その関数で std::Exception を発生させるスタックにオブジェクトを生成する場合、そのオブジェクトは捕捉されず、次の出力を取得し、プログラムは直ちに終了します。

terminate called without an active exception
Aborted

以下は私のコンパイラ呼び出しです。

arm-drm-linux-gnueabihf-g++ -fPIC -pipe -ggdb -o MyLib.o -c MyLib.cpp
arm-drm-linux-gnueabihf-g++ -fPIC -pipe -ggdb -o LibraryLoader.o -c LibraryLoader.cpp
arm-drm-linux-gnueabihf-g++ -g3 -ggdb -Wall -fPIC -pipe -isystem /sysroot/usr/local/include\ -fsigned-char -D_USE_EMBEDDED_ -ffunction-sections -fdata-sections -static-libstdc++ -lpthread -ldl -shared -L/sysroot/usr/local/lib MyLib.o LibraryLoader.o -o myLib.so

arm-drm-linux-gnueabihf-g++ -fPIC -pipe -ggdb -o Main.o -c Main.cpp
arm-drm-linux-gnueabihf-g++ -g3 -ggdb -Wall -fPIC -pipe -isystem /sysroot/usr/local/include\ -fsigned-char -D_USE_EMBEDDED_ -ffunction-sections -fdata-sections -static-libstdc++ -lpthread -ldl -L/sysroot/usr/local/lib  Main.o -o Main.linux-arm

これは私のコードです。

メインプログラム.cpp

#include <iostream>
#include <stdlib.h>
#include <dlfcn.h>
#include <stdio.h>

int main(int argc, char* argv[])
{
    std::string sLibname("myLib.so");
    std::string sInitFuncName = "Foo";

    void *handle = NULL;
    long (*func_Initialize)(void*);
    char *error;

    handle = dlopen(sLibname.c_str(), RTLD_LAZY | RTLD_LOCAL);

    if (!handle) {
        fputs(dlerror(), stderr);
        exit(1);
    }

    *(void**)(&func_Initialize) = dlsym(handle, sInitFuncName.c_str());

    if ((error = dlerror()) != NULL) {
        fputs(error, stderr);
        exit(1);
    }

    printf("Call library function 'Foo'\n");
    func_Initialize(NULL);
    printf("Call library function 'Foo' DONE\n");

    dlclose(handle);
    return 0;
}

MyLib.hpp

extern "C" {
long DEBMIInitialize();
}

MyLib.cpp

#include "LibraryLoader.hpp"
#include "MyLib.hpp"
#include <stdio.h>
#include <exception>

long Foo()
{

    try {
        std::exception e;
        throw e;
    }
    catch (std::exception)
    {
        //This is caught
        printf("Caught std::exception\n");
    }



    try {
        LibraryLoader oLibLoader;
        oLibLoader.Run();
    }
    catch (std::exception)
    {
        //This is not caught
        printf("Caught std::exception from ClLibrayLoader\n");
    }

    return 0;
}

ライブラリローダー.hpp

#include <exception>
#include <stdio.h>

class LibraryLoader
{
public:
    LibraryLoader() {};
    ~LibraryLoader() {};
    void Run() {
        std::exception e;
        throw e;
    };
};

-O1O2編集:最適化のためにコンパイラフラグ(、..)を追加するとO3(2番目の)例外もキャッチされることを確認しました。

ベストアンサー1

例外は "MyLib.cpp::Foo()" にローカルなので、 new() を使用して例外を生成する必要があるかもしれません。

おすすめ記事