[PyKDE] stale C++ pointer?

Chris Jones cdj at mail.lns.cornell.edu
Wed Aug 4 21:46:01 BST 2004

I am new to PyQt but having read through all the documentation I can 
find about object ownership policy with PyQt I believe I have found a 
bug which is causing a Python object to hold a stale C++ pointer.

For reference I am developing on Mac OS X v 10.3.4 using PyQt 3.12, sip 
4.0.1, Mac Qt 3.2.3 and Python 2.3.4.

I have a class that is handed a QMouseEvent in the class' __init__ 
method where I obtained the mouse events position (a QPoint) by doing
				self._start = event.pos()

then in a member method I make use of self._start.  Unfortunately, the 
values returned from self._start.x() and self._start.y() that I get 
when called from the other member method are completely erroneous.  
However, If I change my __init__ method so I explicitly create a new 
QPoint, i.e.

				self._start = QPoint( event.pos() )

then the values returned from self._start.x() (and .y()) are what they 
are supposed to be.

My conclusion is the underlying C++ object returned from the initial 
call to event.pos() is only correct during the scope of the __init__ 
call and has been deleted by the time I entry my other member method, 
even though I have a reference to the wrapping Python object.

Looking at the wrapping code for the QMouseEvent::pos() routine I find

                         const QPoint *sipRes;

                         sipRes = &sipCpp -> QMouseEvent::pos();

                         return sipMapCppToSelf(const_cast<QPoint 

So the question is, is the address held by 'sipRes' the c++ object that 
ultimately gets held by the Python wrapping object?  If so, then it is 
easy to see why that becomes a stale pointer (since the QPoint is 
'deleted' when the QMouseEvent goes out of scope).  If this is not the 
case, then there appears to be another problem with PyQt's C++ object 


More information about the PyQt mailing list