Results 1 to 7 of 7

Thread: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

  1. #1
    Join Date
    Nov 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    I've got a large mature app using Qt 5.6 on Windows 10.

    It loads a QPlugin which interfaces to an interpreted language, and that may load many dlls.
    At some point, I need to Unload this QPlugin and then load a similar but different plugin that lets me use a different version of the interpreted language.

    The problem is that if the first instance of QPlugin loads one version of x.dll, y.dll and z.dll, then the second Plugin tries to use that same dlls, instead of unloading it and using a different version of x.dll, y.dll and z.dll.

    If I knew which .dlls were going to be loaded, I could pre-load them with QLibrary, and unload them after I unload the QPlugin.... but that's not always going to be the case.

    Is there a way I can unload all of the .dlls loaded by my QPlugin?

    (In the visual studio output window, I can see that it usually unloads them, but we're definitely getting errors where it can't find a function that exists in a .dll loaded by the the second QPlugin, and the error tells me it's looking in the loaded-by-first-x.dll)

    Thanks!

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,053
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    It could be that there isn't any way for one DLL to determine if it is the only one dependent on those DLLs or if there are other parts of the app that are also using them. So it can't arbitrarily unload all of the DLLs it depends on, because that might break the application. Only the OS knows for sure which binaries have loaded which dependent DLLs and can unload those after all of the "parent" DLLs have unloaded.

    There might be some Windows-specific API that you can use to determine which DLLs yours has loaded and maybe what the reference counts are for those (if Windows uses reference counting at all to keep track of how many users of a DLL there are), but that's way beyond anything I've ever needed to do.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,396
    Thanks
    37
    Thanked 1,542 Times in 1,493 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    Maybe you can interface with the other language through a helper process instead?

    E.g. either in that language itself or with Qt write a program that communicates with your main program and runs whatever task you want done in the other language.
    The main program can then launch and stop this process when needed, each time triggering a new runtime linking on startup.

    Cheers,
    _

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,053
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    @jimmaplesong: Welcome to the world of "DLL hell". This was supposed to have been solved by the use of Windows 7 and later technology for "side-by-side" (SxS) deployment and manifests, but that might apply only when separate apps use DLLs of the same name but different versions.

    It sounds like you might be dealing with Python 2 / 3 incompatibility problems. Have you looked to see if there are solutions in the Python support community?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Jul 2012
    Posts
    225
    Thanks
    18
    Thanked 14 Times in 13 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    Actually windows does fine unloading dllls from memory, when they are not used anymore. Do you have access to the source code of the plugins? If so check that all libraries are unloaded on exit.

    Are you sure that the paths are as you expect? Try loading the library with full path instead of relying on windows to search for it.

    Note that dlls are always loaded from the working dir of the executable, not from the dir where the plugin.dll file maybe located.

  6. #6
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    4,053
    Thanks
    233
    Thanked 649 Times in 639 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    Note that dlls are always loaded from the working dir of the executable, not from the dir where the plugin.dll file maybe located.
    So you could change the working directory that of the plugin DLL before loading it and it would use whatever DLLs it found there? (That is, Windows will search "." first before searching $PATH)?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  7. #7
    Join Date
    Nov 2016
    Posts
    2
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Need to unload dependent dlls loaded by QPlugin when I unload the plugin.

    Quote Originally Posted by d_stranz View Post
    There might be some Windows-specific API that you can use to determine which DLLs yours has loaded and maybe what the reference counts are for those (if Windows uses reference counting at all to keep track of how many users of a DLL there are), but that's way beyond anything I've ever needed to do.
    Thanks d_. Windows does have hooks so I could be notified whenever a dll loads, and these might be necessary for me to track those dependencies by hand. I hope that's not the case, but it might be a step forward.

    I'm reading up on Qt's IPC capabilities to gauge the effort involved. I do think it's the ultimate solution. Google Chrome uses this strategy, and it works well for them.


    Added after 4 minutes:


    If the dlls that we were using all had good SxS manifests, it would prevent this issue where the 'wrong version' is being utilized. Unfortunately, the pre-SxS dlls are in-the-wild, and the chances of updating the whole infrastructure in any reasonable time-frame are low.

    There's no Python in this project.

    Quote Originally Posted by tuli View Post
    Actually windows does fine unloading dllls from memory, when they are not used anymore. Do you have access to the source code of the plugins? If so check that all libraries are unloaded on exit.

    Are you sure that the paths are as you expect? Try loading the library with full path instead of relying on windows to search for it.

    Note that dlls are always loaded from the working dir of the executable, not from the dir where the plugin.dll file maybe located.
    In the past, we had a strategy where we would copy most of the the dependencies (copying the .dlls) into the same directory as the QPlugin. Before we load the plugin, we would set the CWD to the plugin's directory. Unfortunately, the duplication (& re-distribution of the core libraries) was unacceptable.
    Last edited by jimmaplesong; 22nd July 2019 at 15:36.

Similar Threads

  1. Replies: 1
    Last Post: 24th February 2014, 08:39
  2. How to unload a QDeclarativeView object
    By nestuser in forum Newbie
    Replies: 0
    Last Post: 17th August 2012, 06:45
  3. How to unload a QPixmap
    By MorrisLiang in forum Newbie
    Replies: 1
    Last Post: 19th June 2010, 22:30
  4. Replies: 0
    Last Post: 16th September 2009, 02:33
  5. Use of parent for automatic unload
    By PaceyIV in forum Newbie
    Replies: 9
    Last Post: 30th July 2009, 08:13

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.