[PyQt] Re: memory corruption storing python objects in a QModelIndex with createIndex

Andreas Pakulat apaku at gmx.de
Wed Oct 1 23:18:16 BST 2008


On 01.10.08 12:28:28, Erick Tryzelaar wrote:
> On Wed, Oct 1, 2008 at 4:53 AM, Phil Thompson
> <phil at riverbankcomputing.com> wrote:
> > Try using modeltest.py from the contrib directory.
> 
> Thanks again Phil. That found some problems, but I'm still getting the
> aborts. I'm now getting "Fatal Python error: GC object already
> tracked" with this new script. I've marked where the GC exception
> occurs:
> 
> class Model(QAbstractItemModel):
>     def __init__(self, parent=None):
>         QAbstractItemModel.__init__(self, parent)
> 
>     def rowCount(self, parent=QModelIndex()):
>         return 1

Thats not a good idea, even for testing purposes. This might create and
"infinite" model, especially together with
 
>     def hasChildren(self, index):
>         return True

This.
 
>     def parent(self, index):
>         if not index.isValid():
>             return QModelIndex()
> 
>         node = Node()
>         return self.createIndex(0, 0, node)

Thats bogus too, if its a valid index, it has been created from the index()
function. So you should not create a new node, but instead return the
parent of the internal pointer of that index - if you want to use an
internal pointer.

Apart from that, here the node you give to createIndex is garbage collected
as soon as the return statement is executed.

>     def index(self, row, column, parent=QModelIndex()):
>         if row != 0 or column != 0:
>             return QModelIndex()
>
>         if parent.isValid():
>             parentNode = parent.internalPointer().child()
>         else:
>             node = Node()
> 
>         return self.createIndex(row, column, node)

If the parent index is valid this will throw an exception because you're
accessing a non-existing variable (node) on the return statement.

Andreas 

-- 
Chicken Little was right.


More information about the PyQt mailing list