Thanks in advance for reading this question. Our program encountered a strange issue, the "dlsym" throws a "segment fault" issue sporadically. The background is, our engine loads multiple share libraries into a single process, and stores this libraries's handle which got from "dlopen" into a map,

 void* handler = dlopen(libraryPath, RTLD_NOW);
if (handler != NULL) {
  handlerStore[libraryPath] = handler;
} else {
  throw exception

then later call the functions in these library with below code

void* handler = handlerStore[toStr(libraryPath)];
typedef int (*function)(); // the function to be called
dlerror(); /**clear error code*/

function f = (function) dlsym(handler, functionName);
char *error = dlerror();
if (error != NULL || f == NULL) {
  throw exception

int state_out = 0;
if (f != NULL) {
    state_out = (*f)();
  }catch (...){
    throw exception

The strange behavior is, it works most of the time, but occasionally throws an segment false error with below stack trace.


We are currently struggling on it and really appreciated you can give us some direction


1 Answers

Employed Russian On

"dlsym" throws a "segment fault" issue sporadically.

Since your program uses exceptions, you should use the term throw more carefully: dlsym is implemented in pure-C, and does not throw anything.

But it raises SIGSEGV signal.

The most likely reason for dlsym to raise SIGSEGV is heap corruption, and the best tools for catching such corruption are Address Sanitizer (build with -fsanitize=address if using recent GCC or Clang), or Valgrind.