LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Preventing a DLL (or shared library) from being unloaded

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.

NL

 

 

0 Kudos
Message 1 of 2
(2,986 Views)

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...
  }
}

 

 

 

0 Kudos
Message 2 of 2
(2,960 Views)