PythonQt Wrapper Reference counting
Dynamic Python binding for Qt Applications
Brought to you by:
florianlink,
marcusbarann
Hi!
I have a question regarding reference counting.
I have a C++ object, owned by my app. In PythonQt i have created a wrapper, so i call
registerClass("PyClassName","","", PythonQtCreateObject<cppwrapper>)</cppwrapper>
The class comes into python via another c++ object. There is no wrapper to create or delete it from python.
In python i call
b1 = MyObject.get()
b1 shows to my c++ object (adress)
if i now call b1 = None, i get no hint it is not used any more
how can i detect this?
Is this the last parameter in registerCppClass for ( PythonQtShellSetInstanceWrapperCB*)
or is there a special callback in the wrapper required?
the delete_PyClassName slot in the wrapper is not called ( i assume because not Object created in python)
A long time ago thread from 2010 stated it could also be not possible?
Any hints welcome :-)
The default ownership of returned objects is C++, so your object is not deleted by Python, just the wrapper to it. If your get() method returns a new object and you want Python to delete it, you can use the ownership templates in PythonQt.h
http://pythonqt.sourceforge.net/classPythonQtPassOwnershipToPython.html#aaad0d16be54de03dbd53c5184f1318eb
the templates don't do anything, but tell PythonQt that a returned pointer should change ownership.
The shell callback is something else, it is used to set the wrapper object on the shell class (the shell class allows deriving a class in Python and implementing the virtual methods in Python.
Ok, understand.
In my case i do not give the ownership to python currently, but i could. Then it would get deleted, and so my destructor is called.
http://pythonqt.sourceforge.net/Developer.html in the section with ownership management the return type of my MyObject.get() slot could do this
Question is if this is not the case, so if i do not WANT the object to be deleted, but used in python, CAN my object be INFORMED that the wrapper object is deleted? Or would this be a new feature?
Last edit: Joerg Kreuzberger 2018-09-05
No, there is no such mechanism. PythonQt could have a callback to notify you when an object wrapper is deleted, but it would not know where the object came from.
Hmm, does not work as expected by myself.
The slot has now a return type of
But the destructor of the PyBitView object is never called if i delete the object in python
Is there still a reference hold by the wrapper object?
I currently cannot find the source code line to start debugging on, perhaps you have an hint?
Started from PythonQtSlot call. The result seems to have the right flags.
Last edit: Joerg Kreuzberger 2018-09-06
Hm, does your wrapper have a destructor slot (I forgot how they are named, delete_XYZ)? Because that is what PythonQt will use to destroy it (otherwise it can't destroy it).
and this it was. Argh. i removed the slot BEFORE trying it with this PassOwnerShipStuff.
Many thanks!!!