0

I am cross compiling with gcc 4.9.2 for armv7hv (gcc-4.9.2_armv7hf_glibc-2.9). Theres a main executable and a library with one exported function Foo().

What I experience is that if I throw an exception within that library's function Foo(), and try to catch it right away it's caught.

However if within that function I create an object on the stack that throws a std::exception, it is not caught and I get the following output and the program terminates right away:

terminate called without an active exception
Aborted

These are my compiler calls:

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

This my code:

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

LibraryLoader.hpp

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

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

EDIT: I just noticed that the (2nd) exception is also caught when I add the compiler flag for optimization -O1 (O2, O3..).

0

Maybe you should create the Exception with new(), as it is local in "MyLib.cpp::Foo()".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.