Qt-interest Archive, January 2008
Plugin ctor executes, but not dtor ?
Message 1 in thread
I have a plugin instance that I create using QPluginLoader::instance()
and that I (currently) allow to be unloaded by dropping off the end
of the program i.e. I don't explicitly call QPluginLoader::unload()
and rely on the the fact that this should happen automatically.
It seems that QPluginLoader::instance() causes the plugin's
ctor to execute, but that plugin's dtor never runs.
I'm loading a set of plugins with code like this:
>foreach (QString file_name, plugin_dir.entryList(QDir::Files))
>{
> if (QLibrary::isLibrary(file_name))
> {
> QPluginLoader loader(plugin_dir.absoluteFilePath(file_name));
>
> if (TransferrerInterface *interface = qobject_cast<TransferrerInterface *>(loader.instance()) )
> {
which is based on code in the Blanchette/Summerfied book.
Now, having looked at the docs for QPluginLoader, I'm beginning
to wonder if this code is dubious, since the QPluginLoader
in question goes out of scope long before it gets a chance
to call unload() (and I, of course, can't manually call unload()
for the same reason).
So the questions are:
1. Is the code above strictly incorrect ?
2. As a corollary, should all QPluginLoader objects have the
same lifetime as the instances which they create, in order to
allow unload() to be called ?
3. Is the code above the reason that the plugin dtor is not called ?
--
[ signature omitted ]
Message 2 in thread
Hi Stephen,
I met the same problem before. In Windows platform, the unload() is:
bool QLibraryPrivate::unload_sys()
{
if (!FreeLibrary(pHnd)) {
errorString = QLibrary::tr("QLibrary::unload_sys: Cannot unload
%1 (%2)").arg(fileName).arg(::qt_error_string());
return false;
}
errorString.clear();
return true;
}
It calls Win32 API FreeLibrary(). FreeLibrary() only calls DllMain()
when the dll is detached. So maybe you can put your clean up code is
DllMain(), rather than dtor().
Kefei You
Geomodeling Technology Corp.
1100, 665 - 8 Street S.W.
Calgary, AB T2P 3K7
Canada
-----Original Message-----
From: Stephen Collyer [mailto:scollyer@xxxxxxxxxxxxxxxx]
Sent: Friday, January 11, 2008 8:10
To: Qt Interest List
Subject: Plugin ctor executes, but not dtor ?
I have a plugin instance that I create using QPluginLoader::instance()
and that I (currently) allow to be unloaded by dropping off the end of
the program i.e. I don't explicitly call QPluginLoader::unload() and
rely on the the fact that this should happen automatically.
It seems that QPluginLoader::instance() causes the plugin's ctor to
execute, but that plugin's dtor never runs.
I'm loading a set of plugins with code like this:
>foreach (QString file_name, plugin_dir.entryList(QDir::Files))
>{
> if (QLibrary::isLibrary(file_name))
> {
> QPluginLoader loader(plugin_dir.absoluteFilePath(file_name));
>
> if (TransferrerInterface *interface =
qobject_cast<TransferrerInterface *>(loader.instance()) )
> {
which is based on code in the Blanchette/Summerfied book.
Now, having looked at the docs for QPluginLoader, I'm beginning to
wonder if this code is dubious, since the QPluginLoader in question goes
out of scope long before it gets a chance to call unload() (and I, of
course, can't manually call unload() for the same reason).
So the questions are:
1. Is the code above strictly incorrect ?
2. As a corollary, should all QPluginLoader objects have the same
lifetime as the instances which they create, in order to allow unload()
to be called ?
3. Is the code above the reason that the plugin dtor is not called ?
--
[ signature omitted ]
Message 3 in thread
After a bit more thought, I think I have the answers:
Stephen Collyer wrote:
> 1. Is the code above strictly incorrect ?
No. The destruction of the plugin instance and unloading of
the library are independent; in the code shown earlier, the
QPluginLoader object is destroyed, but the library isn't
unloaded (I think) since the instance will keep a reference
to library alive.
It's up to the app. itself to delete the instance when it's
finished with it.
> 2. As a corollary, should all QPluginLoader objects have the
> same lifetime as the instances which they create, in order to
> allow unload() to be called ?
No. This will still happen automatically when all references
to the library disappear.
> 3. Is the code above the reason that the plugin dtor is not called ?
No. The app. must delete the instance via
delete(instance)
to ensure that this happens. I guess it can be a leak if
the app. doesn't do this appropriately.
--
[ signature omitted ]