import sys import weakref import objgraph from PySide2 import QtCore, QtGui, QtQml, QtQuick class TestType(QtCore.QObject): instances = weakref.WeakSet() def __init__(self): super().__init__() TestType.instances.add(self) self._value = '' self.destroy_closure = lambda: self.handle_destroy() self.destroyed.connect(self.destroy_closure) def handle_destroy(self): print('Type destroyed') self.destroy_closure = None def getValue(self): return self._value def setValue(self, value): print('Set value', value) for i, obj in enumerate(TestType.instances): print('Refcounts', i, repr(obj.value), sys.getrefcount(obj)) objgraph.show_backrefs( [obj], filename='refs-%s.png' % i, refcounts=True) self._value = value valueChanged = QtCore.Signal() value = QtCore.Property('QVariant', getValue, setValue, notify=valueChanged) QtQml.qmlRegisterType(TestType, 'Test', 1, 0, 'TestType') def main(): app = QtGui.QGuiApplication() app.setOrganizationName('Org') engine = QtQml.QQmlApplicationEngine() engine.load('view.qml') app.exec_() if __name__ == "__main__": main()