[Kde-bindings] playground/bindings/kimono

Richard Dale richard.j.dale at gmail.com
Tue Mar 6 14:48:29 UTC 2007


On Tuesday 06 March 2007, Arno Rehn wrote:
> Am Montag, 5. März 2007 schrieb Richard Dale:
> > So the 'internalPointer' has a different value even when the underlying
> > instance is the same. That's because QAbstractItemModel.CreateIndex()
> > allocates a new GCHandle every time, which will have different values.
> > The reason it works in QtRuby is because the internalPointer is a Ruby
> > VALUE that is  the same for every createIndex call.
>
> Ah tanks. I added a Dictionary where all the GCHandles for QModelIndex'es
> are stored. This GCHandle is looked up for the specific QModelIndex and
> then passed to the C++ helper function. Selecting child items is no problem
> anymore. Up to now I thought GCHandle.Alloc() returns always the same
> value, but apparently it doesn't.
I thought about have a WeakReference as a key, but I don't think that works
because wrapping an instance in a GCHandle will probably create a strong
reference (you wouldn't want an instance to be GC'd while you were
referencing it in C code).

So I think you need to have the actual object as a key, and a struct as a
value with two fields; a reference count and a GCHandle. When you add a new
GCHandle, set the ref count to 1 and increment every time the handle is
reused in another QModelIndex. Then in the QModelIndex destructor, retrieve
the entry for the wrapped object and decrement the ref count. If the ref
count is then zero, delete the entry.

-- Richard



More information about the Kde-bindings mailing list