in reply to embedding perl in a shared library crashes if shared lib is load dynamically

What kind of crashes are you experiencing?

Are you sure the shared object is being loaded? After the dlopen you might want to check the value of the handle, for example:
if (!handle) { fprintf (stderr, "%s\n", dlerror()); return 1; }
You probably may wish to check the return value from dlsym as well.

There might be issues using LAZY (although it works for me), but RTLD_NOW is worth a try.

Replies are listed 'Best First'.
Re^2: embedding perl in a shared library crashes if shared lib is loaded dynamically
by lorius (Novice) on Jan 06, 2010 at 13:30 UTC

    thx cdarke, I tried this already before (I removed the testing of the handle in my example to limit the sample files to a strict minimum). I just put it back again for safety, same result.

    Also changing RTLD_LAZY to RTLD_NOW creates no different behaviour (just tried it).

    Loading of the shared lib seems to work correctly as removing the line 'use IO::Socket' from hello.pl and executing ./perlEmbedDynamicLoading generates the expected output '** hello from hello.pl **' (so the loading is ok). But when I put back again the statement "use IO::Socket", ./perlEmbedDynamicLinking works fine (just as runinng "perl hello.pl"), but ./perlEmbedDynamicLoading crashes with the only message "memory Fault".

    when you say "although it works for me", does this mean that you've been able to run the example files without any problem ?

      When I said "works for me" I meant that RTLD_LAZY worked, unfortnately I'm not currently in a position to try your code (maybe tonight). Sorry to have mis-lead you.

      This is pure speculation. The difference with a use statement is that it is executed at compile-time. Could you change the use and require the module after you dynamically loaded your code?

      I also found this Problem with dlopen and shared objects using perl libs, but I don't think it helps much.

        thank you, cdarke ! I already tried to interchange use and require without any success (I really tried a lot of things before). But your link to the dlopen problem invited me to try to combine RTLD_NOW with RTLD_GLOBAL in the dlopen call, and now it works !! you saved my day !

        My fear is that the application in which I want to embed the perl interpreter, misses this option too when loading shared libraries, and unfortunately I have no influence on this (it is a commercial predilivered one). But at least I know where to look and check further (reading about compiling and linking... what a pitty, really prefer spending my time with perl itself :-))