Details
-
Bug
-
Resolution: Unresolved
-
P3: Somewhat important
-
None
-
5.12.2
-
None
-
Windows 10 with Qt 5.12.2 MSVC and .NET Framework v4.6.1
Description
Deleting an instance of QAxObject does not delete the COM object which was created by the same instance with QAxObject::setControl(QString).
As the documentation of the QAxObjects destructor states, the COM object should be released and all allocated resources cleaned up. What actually happens is, that the QAxObject instance is deleted, while the COM object 'survives' until the calling application is shut down completely.
The following code sample can be used to reproduce this behaviour:
// main.cpp #include <QAxObject> #include <QCoreApplication> #include <QDebug> #include <QTimer> #include <combaseapi.h> int main(int argc, char *argv[]) { qInfo("main: Starting test..."); QCoreApplication a(argc, argv); if (SUCCEEDED(CoInitializeEx(NULL, COINIT_MULTITHREADED))) { qInfo("main: CoInitializeEx() succeeded"); QAxObject *axObject = new QAxObject(); axObject->setControl("MyComObject.ComObject"); axObject->dynamicCall("SayHello"); delete axObject; // Message 'ComObject: Object destroyed' is expected to appear around here qInfo("main: QAxObject deleted"); CoUninitialize(); } else { qFatal("main: CoInitializeEx() failed"); } // Properly shut down the calling application QTimer::singleShot(5000, &a, SLOT(quit())); bool ret = a.exec(); qInfo("main: Done..."); return ret; }
Simple code of a COM object example (C#):
// Simple COM object in C# using System; using System.Runtime.InteropServices; namespace MyComObject { [Guid("8157be30-e99a-4b14-b88d-bc8db831047b")] public interface IComObject { [DispId(1)] void SayHello(); } [Guid("6c080298-7b10-494b-84af-38b6db12a7d4"), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)] public interface ComObjectEvents { } [Guid("63d3c24d-2a60-4338-b312-63a771c580bd"), ClassInterface(ClassInterfaceType.None), ComSourceInterfaces(typeof(ComObjectEvents))] public class ComObject : IComObject { public ComObject() { Console.WriteLine("ComObject: Object created"); } ~ComObject() { Console.WriteLine("ComObject: Object destroyed"); } public void SayHello() { Console.WriteLine("ComObject: Hello!"); } } }
Running the main program shown above produces the following output:
// Output of main.cpp main: Starting test... main: CoInitializeEx() succeeded ComObject: Object created ComObject: Hello! main: QAxObject deleted main: Done... ComObject: Object destroyed
The last line 'ComObject: Object destroyed' must not appear after the line 'main: Done...' which apperas with a delay of approximatly 5 seconds but is expected to appear after or around 'main: QAxObject deleted'.
I've added the build com object as attachment, so hazling with C# is not required. .NET Framework 4.6.1 is required though....