[PyKDE] stale C++ pointer?
phil at riverbankcomputing.co.uk
Thu Aug 5 00:24:01 BST 2004
On Wednesday 04 August 2004 8:39 pm, Chris Jones wrote:
> 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.
It's correct while the QMouseEvent instance is valid.
> 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
It's not a bug - it's just the way things work. If Trolltech had given QPoint
a virtual destructor then you would would have got an exception when you
tried to use the QPoint after it had disappeared.
If you need to keep any information from an event, you need to copy it - just
as you did.
More information about the PyQt