From 00ceda30be09ecc7ed178b874c9521048ddc7d4b Mon Sep 17 00:00:00 2001 From: Michael Lamers Date: Thu, 19 Jul 2018 22:00:35 +0200 Subject: [PATCH] A little bit more advanced instance testing in the Sandbox --- src/net/Qt.NetCore.Sandbox/Program.UI.cs | 13 +++- src/net/Qt.NetCore.Sandbox/main.qml | 84 +++++++++++++++++++----- 2 files changed, 81 insertions(+), 16 deletions(-) diff --git a/src/net/Qt.NetCore.Sandbox/Program.UI.cs b/src/net/Qt.NetCore.Sandbox/Program.UI.cs index 31f21d55..14498d4f 100644 --- a/src/net/Qt.NetCore.Sandbox/Program.UI.cs +++ b/src/net/Qt.NetCore.Sandbox/Program.UI.cs @@ -32,6 +32,11 @@ namespace Qt.NetCore.Sandbox { } + + public void Log(string logMessage) + { + Console.WriteLine(logMessage); + } } public class TestQmlInstanceHandling @@ -52,11 +57,17 @@ namespace Qt.NetCore.Sandbox return _instanceType; } - public void DeleteInstances() + public void DeleteInstance() { _instanceType = null; } + public void CreateNewInstance() + { + _instanceType = new InstanceType(); + _weakInstanceTypeRef = new WeakReference(_instanceType); + } + public bool IsInstanceAlive() { return _weakInstanceTypeRef.TryGetTarget(out var _); diff --git a/src/net/Qt.NetCore.Sandbox/main.qml b/src/net/Qt.NetCore.Sandbox/main.qml index ad3b66a4..e5ffa920 100644 --- a/src/net/Qt.NetCore.Sandbox/main.qml +++ b/src/net/Qt.NetCore.Sandbox/main.qml @@ -23,26 +23,80 @@ ApplicationWindow { } Timer { id: instanceCheckTimer + property var instanceRef: null interval: 1000; running: true; repeat: true onTriggered: { - if(testInstances.State == 0) { - var ref1 = testInstances.GetInstance() - var ref2 = testInstances.GetInstance() + switch(testInstances.State) { + case 0: + var ref1 = testInstances.GetInstance() + var ref2 = testInstances.GetInstance() - ref1 = null - ref2 = null + 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!"); + console.log("Created and deleted two references on QML side. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State++ + break + case 1: + testInstances.DeleteInstance() + console.log("Deleting .Net references. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State++ + break + case 2: + if(testInstances.IsInstanceAlive()) { + console.log("Yeah! Instance has been released!"); + testInstances.State++ + } + break + case 3: + testInstances.CreateNewInstance() + console.log("Created new .Net Instance. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State++ + break + case 4: + instanceRef = testInstances.GetInstance() + var secondLocalRef = testInstances.GetInstance() + + console.log("Created two QML refs. One scoped and one long living. IsAlive = " + testInstances.IsInstanceAlive()) + + testInstances.State++ + //secondLocalRef will be freed here + break + case 5: + instanceRef.Log("Long living QML ref still works!") + testInstances.State++ + break + case 6: + instanceRef.Log("Long living QML ref still works!") + testInstances.State++ + break; + case 7: + console.log("Deleting long living QML ref. IsAlive = " + testInstances.IsInstanceAlive()) + instanceRef = null + testInstances.State++ + break; + case 8: + console.log(".Net object IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State++ + break; + case 9: + console.log(".Net object IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.State++ + break; + case 10: + console.log("Releasing .Net instance a second time. IsAlive = " + testInstances.IsInstanceAlive()) + testInstances.DeleteInstance() + testInstances.State++ + break; + case 11: + if(testInstances.IsInstanceAlive()) { + console.log("Yeah! Instance has been released a second time!"); + testInstances.State++ + } + break; + default: instanceCheckTimer.running = false - } + break; } } }