Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to release the plugin's dll file when application is running
Forum Updated to NodeBB v4.3 + New Features

How to release the plugin's dll file when application is running

Scheduled Pinned Locked Moved Solved General and Desktop
22 Posts 7 Posters 3.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q qtyangyong

    i use pluginloader to load and unload dll file. but the dll file can not be deleted even after QPluginLoader::unload returned true.
    what's the matter with below program?

    QPluginLoader loader("ExtraFiltersPlugin.dll");
    plugin = loader.instance();
    if (plugin)
    {
    qDebug() << "plugin";
    FilterInterface *fff = qobject_cast<FilterInterface *>(plugin);
    fff->test();
    }
    int ret = loader.unload();
    qDebug() << ret;
    qDebug() << QFile::remove("ExtraFiltersPlugin.dll"); // delete failed

    thanks.

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #5

    @qtyangyong Why do you want to delete the plug-in file?!

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    1 Reply Last reply
    0
    • Q Offline
      Q Offline
      qtyangyong
      wrote on last edited by
      #6

      sometimes, It is necessary to modify the DLL when work app is still running.
      the online update function of plug-in architecture

      1 Reply Last reply
      0
      • Q qtyangyong

        i use pluginloader to load and unload dll file. but the dll file can not be deleted even after QPluginLoader::unload returned true.
        what's the matter with below program?

        QPluginLoader loader("ExtraFiltersPlugin.dll");
        plugin = loader.instance();
        if (plugin)
        {
        qDebug() << "plugin";
        FilterInterface *fff = qobject_cast<FilterInterface *>(plugin);
        fff->test();
        }
        int ret = loader.unload();
        qDebug() << ret;
        qDebug() << QFile::remove("ExtraFiltersPlugin.dll"); // delete failed

        thanks.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #7

        @qtyangyong
        Why do you ask this question again, with the same code, as I distinctly recall you asking a week or so ago, to which you got answers?

        OK, hold on: I see you have only made 3 posts. Exactly this question with exactly the same code was asked by somebody a week or so ago. Was it not you? Looks like identical code....

        You might search the form for that thread. The gist was that under Windows you are liable not to be able to delete a DLL once loaded. You need to call setLoadHints() with the QLibrary::PreventUnloadHint flag cleared I think. But even then an answer someone gave to the other thread indicated it may not work, and you're not supposed to unload plugins/delete their DLL file till after program exit, I believe.

        UPDATE
        The other thread in question was https://forum.qt.io/topic/136303/how-to-release-the-plugin-s-dll-file-when-application-is-running. Oh, that is indeed you! What a waste of us responders' time :(
        Your code does not even show what you were told to try from there. So you already asked just this question and got your answer, why do you make people have to answer again in a new thread? Do you think that will generate the answer you want it to now?

        1 Reply Last reply
        3
        • Q Offline
          Q Offline
          qtyangyong
          wrote on last edited by
          #8

          i'am very very sorry,I haven't seen that reply before
          by the way, I tried to use ploader - > setloadhints (0); Still can't succeed
          It is said that it can work normally in the historical version of QT
          please read this: https://ask.csdn.net/questions/1059003

          jsulmJ 1 Reply Last reply
          0
          • Q qtyangyong

            i'am very very sorry,I haven't seen that reply before
            by the way, I tried to use ploader - > setloadhints (0); Still can't succeed
            It is said that it can work normally in the historical version of QT
            please read this: https://ask.csdn.net/questions/1059003

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @qtyangyong I doubt @JonB can read Chinese, I also can't.
            "historical version of QT" - what does this mean? Do you mean old Qt version? What Qt version do you use?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • Q Offline
              Q Offline
              qtyangyong
              wrote on last edited by
              #10

              https://ask.csdn.net/questions/1059003
              In that article, it said Qt5 4.2 has no such problems.
              I try to use 5.12.10, 5.12.12 and 5.15.2, all have such problems

              1 Reply Last reply
              0
              • Q Offline
                Q Offline
                qtyangyong
                wrote on last edited by JKSH
                #11

                Because of repeated questions, this question is merged into https://forum.qt.io/topic/136392/how-to-release-the-plugin-s-dll-file/7 , please don't reply here, thank you.
                by the way, the dll file can not be deleted event if pluginLoader.setLoadHints(0); is called.

                [EDIT: Both topics are now merged. --JKSH]

                1 Reply Last reply
                0
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  Unloading a plugin will not remove it from the internal structures since there might be still references to this plugin somewhere so it might crash otherwise. There is a lengthy discussion about this on the Qt dev mailing list some years ago. Was in conjunction with QStringLiteral iirc.

                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                  Visit the Qt Academy at https://academy.qt.io/catalog

                  1 Reply Last reply
                  0
                  • Q Offline
                    Q Offline
                    qtyangyong
                    wrote on last edited by
                    #13

                    With regard to the use of dynamic libraries, the basic principle is to release when the last user exits. If it is not released, the dynamic library cannot be hot updated. In this way, we need to add a dynamic library version control module in the application, use a new name for each update, and delete useless files when the application restarts.

                    by the way, I tried, as long as loader->instance(); is called, the DLL file is locked. It has nothing to do with the call of loader->unload(). Is it possible that the unload method did not close the handle of the DLL file?

                    1 Reply Last reply
                    0
                    • kkoehneK Offline
                      kkoehneK Offline
                      kkoehne
                      Moderators
                      wrote on last edited by
                      #14

                      @qtyangyong said in How to release the plugin's dll file?:

                      With regard to the use of dynamic libraries, the basic principle is to release when the last user exits. If it is not released, the dynamic library cannot be hot updated. In this way, we need to add a dynamic library version control module in the application, use a new name for each update, and delete useless files when the application restarts.

                      I learned recently, that, while you cannot delete a .dll or .exe on Windows that is currently used, you can rename it (as long as the library or executable is still in the same folder).

                      So maybe you can just rename the .dll, extract the new .dll, and after a restart delete the old .dll?

                      Director R&D, The Qt Company

                      Q 1 Reply Last reply
                      3
                      • Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #15

                        You should take a look at QLibrary::setLoadHints()

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        Q 1 Reply Last reply
                        0
                        • hskoglundH Online
                          hskoglundH Online
                          hskoglund
                          wrote on last edited by
                          #16

                          Hi, on Windows removing .dll and .exe files that have been recebtly used is not likely to succeed due tio caching etc.

                          Why don”t you instead try renaming your plugin file to something like DeleteMe.dll? That usually works. After that, you can create a new copy with the original filename, and then you”ll have full read/write/delete acccess.

                          JonBJ 1 Reply Last reply
                          2
                          • kkoehneK kkoehne

                            @qtyangyong said in How to release the plugin's dll file?:

                            With regard to the use of dynamic libraries, the basic principle is to release when the last user exits. If it is not released, the dynamic library cannot be hot updated. In this way, we need to add a dynamic library version control module in the application, use a new name for each update, and delete useless files when the application restarts.

                            I learned recently, that, while you cannot delete a .dll or .exe on Windows that is currently used, you can rename it (as long as the library or executable is still in the same folder).

                            So maybe you can just rename the .dll, extract the new .dll, and after a restart delete the old .dll?

                            Q Offline
                            Q Offline
                            qtyangyong
                            wrote on last edited by
                            #17

                            @kkoehne yes, this is a good idea

                            1 Reply Last reply
                            0
                            • hskoglundH hskoglund

                              Hi, on Windows removing .dll and .exe files that have been recebtly used is not likely to succeed due tio caching etc.

                              Why don”t you instead try renaming your plugin file to something like DeleteMe.dll? That usually works. After that, you can create a new copy with the original filename, and then you”ll have full read/write/delete acccess.

                              JonBJ Online
                              JonBJ Online
                              JonB
                              wrote on last edited by
                              #18

                              @hskoglund said in How to release the plugin's dll file when application is running:

                              Hi, on Windows removing .dll and .exe files that have been recebtly used is not likely to succeed due tio caching etc.

                              Exactly!

                              1 Reply Last reply
                              0
                              • hskoglundH Online
                                hskoglundH Online
                                hskoglund
                                wrote on last edited by hskoglund
                                #19

                                Sorry didn't read the above ...please don't reply here.. (use the other forum post), also I see in that post that @kkoehne already suggested the same thing, i.e. renaming the plugin :-)

                                1 Reply Last reply
                                2
                                • Christian EhrlicherC Christian Ehrlicher

                                  You should take a look at QLibrary::setLoadHints()

                                  Q Offline
                                  Q Offline
                                  qtyangyong
                                  wrote on last edited by
                                  #20

                                  @Christian-Ehrlicher
                                  thans, I tried it but didn't solve the problem. Maybe the usage is incorrect.
                                  such as
                                  loader.setloadhints (0);
                                  loader.unload();

                                  Christian EhrlicherC 1 Reply Last reply
                                  0
                                  • Q qtyangyong

                                    @Christian-Ehrlicher
                                    thans, I tried it but didn't solve the problem. Maybe the usage is incorrect.
                                    such as
                                    loader.setloadhints (0);
                                    loader.unload();

                                    Christian EhrlicherC Offline
                                    Christian EhrlicherC Offline
                                    Christian Ehrlicher
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #21

                                    @qtyangyong said in How to release the plugin's dll file when application is running:

                                    Maybe the usage is incorrect.

                                    For sure - they're load hints, not unload hints... set it before you load the library.

                                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                    Visit the Qt Academy at https://academy.qt.io/catalog

                                    1 Reply Last reply
                                    2
                                    • Q Offline
                                      Q Offline
                                      qtyangyong
                                      wrote on last edited by
                                      #22

                                      thank you very much, I moved "loader.setLoadHints(0);" before "loader.instance();", the problem was solved. the program such as below:
                                      int main(int argc, char *argv[])
                                      {
                                      ......
                                      QPluginLoader loader("ExtraFiltersPlugin.dll");
                                      loader.setLoadHints(0);
                                      QObject *plugin = loader.instance();
                                      if (plugin)
                                      {
                                      FilterInterface *fff = qobject_cast<FilterInterface *>(plugin);
                                      fff->test();
                                      }
                                      MainWindow w;
                                      w.ploader = &loader;
                                      ......
                                      }

                                      void MainWindow::on_unloadBtn_clicked()
                                      {
                                      ploader->unload();
                                      }

                                      1 Reply Last reply
                                      0

                                      • Login

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • Users
                                      • Groups
                                      • Search
                                      • Get Qt Extensions
                                      • Unsolved