Sandbox test for instance ref handling

This commit is contained in:
Michael Lamers 2018-07-19 21:39:08 +02:00
parent 0f984313d6
commit d216e9309e
2 changed files with 69 additions and 1 deletions

View file

@ -25,6 +25,43 @@ namespace Qt.NetCore.Sandbox
{
}
public class InstanceType
{
public InstanceType()
{
}
}
public class TestQmlInstanceHandling
{
private InstanceType _instanceType;
private WeakReference<InstanceType> _weakInstanceTypeRef;
public int State { get; set; } = 0;
public TestQmlInstanceHandling()
{
_instanceType = new InstanceType();
_weakInstanceTypeRef = new WeakReference<InstanceType>(_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<TestQmlImport>();
QQmlApplicationEngine.RegisterType<TestQmlImport>("test");
QQmlApplicationEngine.RegisterType<TestQmlInstanceHandling>("testInstances");
engine.Load("main.qml");
return app.Exec();

View file

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