diff --git a/src/net/Qt.NetCore.Sandbox/Program.UI.cs b/src/net/Qt.NetCore.Sandbox/Program.UI.cs index 2ab7413e..31f21d55 100644 --- a/src/net/Qt.NetCore.Sandbox/Program.UI.cs +++ b/src/net/Qt.NetCore.Sandbox/Program.UI.cs @@ -25,6 +25,43 @@ namespace Qt.NetCore.Sandbox { } + + public class InstanceType + { + public InstanceType() + { + + } + } + + public class TestQmlInstanceHandling + { + private InstanceType _instanceType; + private WeakReference _weakInstanceTypeRef; + + public int State { get; set; } = 0; + + public TestQmlInstanceHandling() + { + _instanceType = new InstanceType(); + _weakInstanceTypeRef = new WeakReference(_instanceType); + } + + public InstanceType GetInstance() + { + return _instanceType; + } + + public void DeleteInstances() + { + _instanceType = null; + } + + public bool IsInstanceAlive() + { + return _weakInstanceTypeRef.TryGetTarget(out var _); + } + } static int Main() { @@ -43,7 +80,8 @@ namespace Qt.NetCore.Sandbox var type = NetTypeManager.GetTypeInfo(); QQmlApplicationEngine.RegisterType("test"); - + QQmlApplicationEngine.RegisterType("testInstances"); + engine.Load("main.qml"); return app.Exec(); diff --git a/src/net/Qt.NetCore.Sandbox/main.qml b/src/net/Qt.NetCore.Sandbox/main.qml index 01e9fe76..ad3b66a4 100644 --- a/src/net/Qt.NetCore.Sandbox/main.qml +++ b/src/net/Qt.NetCore.Sandbox/main.qml @@ -2,6 +2,7 @@ import QtQuick 2.7 import QtQuick.Controls 2.0 import QtQuick.Layouts 1.0 import test 1.0 +import testInstances 1.0 ApplicationWindow { visible: true @@ -20,9 +21,38 @@ ApplicationWindow { o2.testSignal("Hello") } } + Timer { + id: instanceCheckTimer + interval: 1000; running: true; repeat: true + onTriggered: { + if(testInstances.State == 0) { + var ref1 = testInstances.GetInstance() + var ref2 = testInstances.GetInstance() + + ref1 = null + ref2 = null + + console.log("Created and deleted two references on QML side. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State = 1 + } else if(testInstances.State == 1) { + testInstances.DeleteInstances() + console.log("Deleting .Net references. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State = 2 + } else if(testInstances.State == 2) { + if(testInstances.IsInstanceAlive()) { + console.log("Yeah! Instance has been released!"); + instanceCheckTimer.running = false + } + } + } + } } TestQmlImport { id: test } + + TestQmlInstanceHandling { + id: testInstances + } }