[PyKDE] Model indexes and internal pointers

David Boddie david at boddie.org.uk
Mon Jul 17 23:54:20 BST 2006


On Tue Jul 18 00:23:47 MEST 2006, Phil Thompson wrote:
> On Monday 17 July 2006 10:09 pm, Arve Knudsen wrote:
> > On 7/17/06, Andreas Pakulat <apaku at gmx.de> wrote:
> > > On 17.07.06 22:00:41, Arve Knudsen wrote:
> > > > Ok, I found the original thread. There is no explanation as to why no
> > > > extra reference is kept however.
> > >
> > > I don't know too much about python refcounting, but I think one problem
> > > could be what to do if the model index disappears. Also decrease the
> > > reference count? This could lead to a problem if "something" else still
> > > needs the object... Phil needs to answer this.

The problem is that ordinary QModelIndexes are temporary, not persistent,
objects. Although it sounds like a good idea to create Python objects,
transfer their ownership to model indexes, and pass those back into C++,
those objects will end up being collected if there are no references from
Python objects.

The point of passing references to Python objects is to help map model
indexes back to those objects later. It doesn't mean very much if those
objects no longer exist. The trick might work for brief communications
between a model and a view, for example, but those Python objects are
often part of a persistent structure implemented in Python so you
normally keep references to them anyway.

> > > > There might be a good reason for this
> > > > behaviour, but it does represent a rather nasty pitfall.
> > >
> > > Well, that's probably a reason why it was introduced so late, you had to
> > > use internalId before.
> >
> > My impression from reading the thread is that it was non-trivial to
> > implement, but added out of a real need, expressed by users.
> 
> It was trivial to implement, once it was pointed out to me that the original 
> (automated) wrapping was useless given the way it is supposed to be used.
> 
> There are plenty of other places where you have to keep an external
> reference to stop an object being garbage collected causing a crash. I just
> need to decide if this is worth making a special case.

I'm in favour of allowing model indexes to take references to Python objects,
as long as everyone realises that you can't store objects in indexes and
expect to get them back later. However, model indexes are supposed to be
lightweight objects and there may be a performance penalty to pay for adding
extra code to manage object reference counting.

David




More information about the PyQt mailing list