[erlang-questions] Problem Loading Shared (.so) Library Within Linked-in Driver

Peter McCarthy <>
Mon Nov 24 11:54:08 CET 2008


Dear all

I've developed a linked-in driver which I load and unload using the
functions erl_ddll:load_driver / open_port and port_close /
erl_ddll:unload_driver respectively.

In the driver's start function (called when a port is opened), I load
a shared (.so) library using dlopen. Then, in the driver's command
function, I call various functions in that shared library. Finally, in
the driver's stop function (called when a port is closed), I use
dlclose to unload the shared library.

Now, everything works okay in general. However, I've noticed the
following problem. If I repeatedly load and unload the linked-in
driver within the same emulator instance, the dlopen function will
eventually fail (always on the 29th call) with the following error:

    "cannot allocate memory in static TLS block"

When this happen, a restart of the emulator is required. Does anyone
know what's happening here?

I notice that linked-in driver functions such as start, command and
stop must be declared static. Is the fact that I'm working with shared
libraries within static functions the cause of the problem in some
way? Also, I've noticed that if I comment-out the call to dlclose, the
problem 'goes away'. One final thing I've noticed is that erl_driver.h
includes prototypes such as driver_dl_open and driver_dl_close. I've
tried using these instead, but couldn't get them to work (they are
undocumented, though).

Any help would be gratefully received.

Peter



More information about the erlang-questions mailing list