A little bit more advanced instance testing in the Sandbox

This commit is contained in:
Michael Lamers 2018-07-19 22:00:35 +02:00
parent d216e9309e
commit 00ceda30be
2 changed files with 81 additions and 16 deletions

View file

@ -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>(_instanceType);
}
public bool IsInstanceAlive()
{
return _weakInstanceTypeRef.TryGetTarget(out var _);

View file

@ -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;
}
}
}