[PyQt] Segfault with QString
phil at riverbankcomputing.com
Fri Mar 21 12:32:46 GMT 2008
On Friday 21 March 2008, Filip Gruszczyński wrote:
> > This is the same issue that came up the other day in the context of
> > events. The QString that value is wrapping is a temporary, so you either
> > need to convert it (as you do when calling str()) or copy it (by calling
> > QString()).
> Could you explain it a bit further or send me to a proper
> documentation? I would like to understand it better, so I would really
> appreciate some clear explanation.
When PyQt wraps a const reference to a QString it just saves the address of
that QString in the Python object. If the QString is then destroyed the
pointer becomes invalid and may segfault if it is subsequently dereferenced.
The solution is to copy the QString before it is destroyed.
A better solution might be for PyQt to automatically copy such things when
wrapping them. This would also avoid another problem where it is possible to
modify a const C++ instance from Python. I haven't yet convinced myself that
such a change will be safe (ie. won't break existing code).
More information about the PyQt