[Interest] Is it ok to use QLibrary to load and unload different shared libraries in sequence?

Thiago Macieira thiago.macieira at intel.com
Wed Oct 24 17:19:44 CEST 2018

On Wednesday, 24 October 2018 02:58:25 PDT Roland Winklmeier wrote:
> At runtime, depending on the user configuration, any of those versions are
> loaded and symbols resolved using e.g. QLibrary
> myDll("ApplicationConnect-4.0"). That all works perfectly fine. Now I would
> like to add the feature to switch versions at runtime and here is my
> question:
> Is it fine to reuse the same QLibrary instance in the following sequence:
> QLibrary myDll("ApplicationConnect-4.0");
> myDll.load()
> [... resolve and do stuff ...]
> [... User configured different version...]
> myDll.unload
> myDll.setFileName("ApplicationConnect-4.1")
> myDll.load()
> [ ... resolve symbols and continue using it...]
> or shall I destroy the previous QLibrary instance and create a new one?

This should work.

> I got some strange behavior while debugging the above lines in QtCreator
> (error message saying that "Command aborted" after the first unload and gdb
> aborts). So I'm not sure if I do something totally wrong.

The problem might be a number of different things. In general, don't unload 
libraries. If your design requires it, rethink and redesign.

Things to watch out for:
1) The number of load() and unload() calls must be paired, since they control 
a refcount. Don't just destroy the QLibrary without unload().

2) make sure that there are no pointers remaining to any function, constant or 
variable left that points to something in that library

3) make sure that no plugin was loaded that linked to that library or, if it 
was, unload it too (preferably, don't unload anything).

4) make sure the libraries don't do anything stupid in their DllMain unattach 

Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel Open Source Technology Center

More information about the Interest mailing list