In Ubuntu 14.04, I have a C++ API as a shared library which I am opening using dlopen
, and then creating pointers to functions using dlsym
. One of these functions CloseAPI
releases the API from memory. Here is the syntax:
void* APIhandle = dlopen("Kinova.API.USBCommandLayerUbuntu.so", RTLD_NOW|RTLD_GLOBAL);
int (*CloseAPI) = (int (*)()) dlsym(APIhandle,"CloseAPI");
If I ensure that during my code, the CloseAPI
function is always called before the main
function returns, then everything seems fine, and I can run the program again the next time. However, if I Ctrl-C
and interrupt the program before it has had time to call CloseAPI
, then on the next time I run the program, I get a return error whenever I call any of the API functions. I have no documentation saying what this error is, but my intuition is that there is some sort of lock on the library from the previous run of the program. The only thing that allows me to run the program again, is to restart my machine. Logging in and out does not work.
So, my questions are:
1) If my library is a shared library, why am I getting this error when I would have thought a shared library can be loaded by more than one program simultaneously?
2) How can I resolve this issue if I am going to be expecting Ctrl-C
to be happening often, without being able to call CloseAPI
?