Note that there are some explanatory texts on larger screens.

plurals
  1. POtypeinfo, shared libraries and dlopen() without RTLD_GLOBAL
    primarykey
    data
    text
    <p>I'm having some trouble with exceptions not functioning correctly (or at least, as I would hope; I know there are issues with this) across shared libraries when loaded using <em>dlopen</em>. I include some simplified example code here. The actual situation is <em>myapp</em>=Matlab, <em>myext1</em>=mexglx matlab extension, <em>mylib</em> is a shared library of my code between the two extensions (<em>myext1</em>, <em>myext2</em>)</p> <p><strong>mylib.h</strong></p> <pre><code>struct Foo { Foo(int a); m_a; } void throwFoo(); </code></pre> <p><strong>mylib.cpp</strong></p> <pre><code>#include "mylib.h" Foo::Foo(int a): m_a(a) {} void throwFoo() { throw Foo(123); } </code></pre> <p><strong>myext1.cpp</strong></p> <pre><code>#include "mylib.h" #include &lt;iostream&gt; extern "C" void entrypoint() { try { throwFoo(); } catch (Foo &amp;e) { std::cout &lt;&lt; "Caught foo\n"; } } </code></pre> <p><strong>myext2.cpp</strong> Identical to myext1.cpp</p> <p><strong>myapp.cpp</strong></p> <pre><code>#include &lt;dlfcn.h&gt; int main() { void *fh1 = dlopen("./myext1.so",RTLD_LAZY); void *fh2 = dlopen("./myext2.so",RTLD_LAZY); void *f1 = dlsym(fh1,"entrypoint"); void *f2 = dlsym(fh2,"entrypoint"); ((void (*)())func1)(); // call myext1 (A) ((void (*)())func2)(); // call myext2 (B) } </code></pre> <p>Compiling this code:</p> <pre><code>g++ mylib.cpp -fPIC -o libmylib.so -shared g++ myext1.cpp -fPIC -o myext1.so -shared -L. -lmylib -Wl,-rpath=. g++ myext2.cpp -fPIC -o myext2.so -shared -L. -lmylib -Wl,-rpath=. g++ myapp.cpp -fPIC -o myapp -ldl </code></pre> <p>The call to <em>entrypoint()</em> at <strong>A</strong> works as expected, with <em>throwFoo()</em> throwing the exception and <em>entrypoint()</em> catching it. The call at <strong>B</strong> however fails to catch the exception. Adding more diagnostic code shows that the typeinfo for the <em>Foo</em> class differs in the two extensions. Changing the order of the two <em>dlopen</em> calls makes no difference, the second loaded extension fails. </p> <p>I know I can fix this by using <strong>RTLD_GLOBAL</strong> as an additional flag for <em>dlopen</em>, but the application (Matlab) using dlopen is out of my control. Is there anything I can do with <em>mylib</em> or <em>myext1</em>, <em>myext2</em> to fix this problem? </p> <p>I have to avoid using LD flags for runtime (since I cannot control the users running the Matlab binary). Any other suggestions?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload