[PyQt] QAbstractItemModel causes segfault

Phil Thompson phil at riverbankcomputing.com
Fri Mar 26 11:08:40 GMT 2010


On Thu, 25 Mar 2010 10:03:41 +0100, "Sebastian Elsner"
<sebastianelsner at freenet.de> wrote:
> Hello,
> 
> I am using a QTreeView to display data coming from a sqlalchemy query.
All 
> 
> is fine for displaying, but when I add rows, which also adds records to  
> the database the "internal pointers", the QAbstractItemModel so heavily  
> depends on, suddenly point to nowhere, because they don't keep strong  
> references (crashing the interpreter). I am using the reference  
> implementation of the editable tree example, with the small adjustment  
> that instead of TreeItem my sqlalchemy class is used. So, at first it  
> seemed like the error was produced by sqlalchemy, but with the help of
the 
> 
> people on the other list I could verify, that its a problem of PyQt.
> 
> To quote Conor from the sqlalchemy list:
> 
>> I believe the real problem is that these "internal pointers" are not
>> doing proper incref/decref on the python objects they point to, so your
>> python objects are getting garbage collected prematurely. The
>> expire-on-commit readily exposes this issue, but disabling it will not
>> really fix the underlying problem.
> 
>> A newer version of pyQT may do the incref/decref for you. If not, your
>> QAbstractItemModel-derived object should keep strong references to
>> everything that was pointed to by an "internal pointer" in a private
>> list or set. This should prevent them from being garbage collected.
> 
> I followed conors suggestion with the string reference list and it
worked. 
> 
> The objects are not garbage collected, the tree builds and extends  
> properly. Is there any pyqt developer, who can share his thoughts on this
 
> and maybe help. I don't want to scream: BUG! but to Michael Bayer, the  
> creator of sqlalchemy this is one:
> 
>> if QT is maintaining a "reference" to something using its "memory
>> address", but is not actually recording a strong reference to the object
>> within the python interpreter, that sure sounds like a bug to me.   It
>> would imply that to use that library, every object you generate in
Python
>> must have a strong reference maintained, or QT now references invalid  
>> ids.

Can you provide some code that demonstrates your use case?

The internal pointers should only be used within a model and you would
expect that the actual objects would be owned by the model. Therefore you
would only need to keep a strong reference to the model itself.

Phil


More information about the PyQt mailing list