How to release the plugin's dll file when application is running
-
[2 topics merged --JKSH]
i want to delete the plugin's dll file when application is running, but i can not work. even if it is successfully unloaded by pluginLoader(return value is true).
when deleting dll file it is still prompted that the file is occupied.
the dll file can be successfully deleted after the application closed.
QT's version is 5.12.12.
the sample demo is shown below:QPluginLoader loader("ExtraFiltersPlugin.dll");
plugin = loader.instance();
if (plugin)
{
qDebug() << "plugin";
FilterInterface *fff = qobject_cast<FilterInterface *>(plugin);
fff->test();
}
loader.unload();MainWindow w;
w.show();
return a.exec(); -
@qtyangyong
Hello and welcome.The first thing to see is the return result from your
loader.unload();
?The second thing is to check whether if you remove the
fff->test();
line that makes any difference.The third thing --- FWIW, may make no difference --- is to destroy/let go out of scope your
QPluginLoader loader
instance, in case that helps.Finally I know it used to be that Windows had a lot of trouble/did not support a DLL being actually unloaded once it had been loaded, only exit from program properly allowed it to be released, e.g. so that it could be deleted. I don't know whether that is still the case.
-
@qtyangyong you don't and you're not supposed to
QPluginLoader sets this hint:
QLibrary::PreventUnloadHint;
you can try via
pluginLoader.setLoadHints(0);
and then unload the library, but use this on your own risk.But be careful about memory managment, you have to be sure that all memory created by the plugin are never used after the unload
This includes any registered metatypes. Be careful because the new-style signal-slot connection automatically registers, as do some functions in QVariant.
-
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 failedthanks.
-
@qtyangyong Why do you want to delete the plug-in file?!
-
sometimes, It is necessary to modify the DLL when work app is still running.
the online update function of plug-in architecture -
@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? -
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 -
@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://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 -
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]
-
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.
-
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?
-
@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?
-
You should take a look at QLibrary::setLoadHints()
-
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.
-
@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!
-
@Christian-Ehrlicher
thans, I tried it but didn't solve the problem. Maybe the usage is incorrect.
such as
loader.setloadhints (0);
loader.unload();