1

I am trying to dlopen memory allocators at runtime. I have no problem with libc, tcmalloc and tbbmalloc. But trying to dlopen jemalloc results in the following error (caught via dlerror) :

/path/to/lib/libjemalloc.so: cannot allocate memory in static TLS block

Do you have any idea of the reason for this error and hence how I could tackle this ?

EnzoMolion
  • 949
  • 8
  • 25
  • I do not think those libraries are designed to be open with `dlopen`. – SergeyA Jun 13 '18 at 14:21
  • Seems like an issue in jemalloc version you have. In any case this is not really good idea to use dynamic library loading nether dlopen nor LoadLibrary for memory allocators. Imagine what is going to happen, when you call to a `free` function when allocator is already unloaded. Link shared libraries to the executable instead. – Victor Gubin Jun 13 '18 at 14:31
  • @SergeyA What do you mean by not "designed to be open with `dlopen`", please ? @VictorGubin I am aware of this pitfall. I am trying to design a subsystem to be able to use multple allocation strategies within the same program. Linking sharing libraries is thus not enough and making sure to free allocated memory with the good allocator is part of the program design. – EnzoMolion Jun 13 '18 at 14:58
  • They are designed to be linked automatically through dynamic loader and become available before program starts executing. – SergeyA Jun 13 '18 at 15:03
  • One has to use `dlsym()` to retrieve the `calloc()`, `free()`, `malloc()` and `realloc()` functions provided by the allocator dynamic library *and* one has to be very careful not to free or reallocate a pointer provided by one allocator with the functions of another allocator. – Yann Droneaud Jun 13 '18 at 20:13

1 Answers1

2

I was able to find a solution to this thanks to jemalloc's GitHub repository issue #1237.

This solution was to recompile jemalloc using the --disable-initial-exec-tls according to the INSTALL.md, my bad.

EnzoMolion
  • 949
  • 8
  • 25