09-26-2012 08:46 AM - edited 09-26-2012 08:55 AM
Dear LabVIEW gurus,
Is there a way to prevent a DLL (or shared library) from being unloaded when the last "associated" Vi is closed?
The idea is to keep the DLL ("locked") in memory till LabVIEW exits.
For advanced DLLs (with several dependencies or highly multithreaded) this is almost mandatory to prevent crashes.
The fact that we usually can't control the order in which the "depency chain" is unloaded is a very classical source of problems.
Matlab (or Igor Pro) has such "DLL locking" mecanism. Is there any workaround in LabVIEW (for both Linux & Windows)?
Thanks for your help.
09-27-2012 04:23 AM - edited 09-27-2012 04:27 AM
I finally found a simple and efficient solution - at least under Linux/gcc - still have to find something similar for windows (same approach might work).
The idea is just to do a self-load!
For the following to work properly you need:
- glibc >= 2.2
- your shared library install directory to be in the LD_LIBRARY_PATH (so that the dynamic linker can find it)
//========================================================== // GNU-LINUX SHARED LIB INITIALIZATION ROUTINE //========================================================== void init_shared_lib (void) __attribute__((constructor)); void init_shared_lib (void) { try { static bool __dll_locked__ = false; if ( ! __dll_locked__ ) { int dlopen_flag = RTLD_LAZY | RTLD_NODELETE; void* dll_hdl = dlopen("tango_binding.dll", dlopen_flag); if ( ! dll_hdl ) { //- Oops... } __dll_locked__ = true; } //- initialize/instanciate your objects here... ObjectManager::init(); } catch (...) { //- Oops... } } //========================================================== // GNU-LINUX SHARED LIB CLEANUP ROUTINE //========================================================== void cleanup_shared_lib (void) __attribute__((destructor)); void cleanup_shared_lib (void) { try { //- release any allocated resource here ObjectManager::fini(); } catch (...) { //- Oops... } }