[PyQt] Segfault with QString

Filip Gruszczyński gruszczy at gmail.com
Fri Mar 21 18:09:03 GMT 2008


Copying would be much more intuitive in my opinion. Thanks for your
help, now I know a bit more, about what happens inside.

2008/3/21, Phil Thompson <phil at riverbankcomputing.com>:
> 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).
>
>
>  Phil
>
>  _______________________________________________
>  PyQt mailing list    PyQt at riverbankcomputing.com
>  http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>


-- 
Filip Gruszczyński



More information about the PyQt mailing list