[Kde-bindings] Questions on TODO list

Richard Dale rdale at foton.es
Wed Feb 7 19:49:32 UTC 2007


On Wednesday 07 February 2007, Arno Rehn wrote:
> Hi,
>
> I've just taken a look at the TODO list, but the first point I don't really
> get:
>
> * Implement a way of ensuring instances with 'owners' like
> QLayoutItem, QListWidgetItem, QTableWidgetItem, QWidget and QObject
> are not garbage collected when they are no referenced in the C# code.
> This matters if they have been subclassed in C#, as when they are
> referenced again and a new C# wrapper instance is created it will
> have lost the customised subclassing.
>
> What exactly do you mean? Maybe an example could do :-)
TODO:
* Make the TODO list comprehensible.. :)

QObjects and QWidgets can have parents, and so they form a tree of objects. In 
C++ when a QObject is deleted, it deletes all its children. So one problem we 
have to solve is to ensure that when that happens in C#, we don't try to 
access methods on the children. On the other hand, you can add a child 
instance with a parent, but not keep any reference to the child in the C# 
code. For instance, in tutorial t14 this line doesn't keep any reference to 
the new QKeySequence, but it is owned by 'this' which is GameBoard a QWidget:

new QShortcut(new QKeySequence((int) Qt.Key.Key_Enter), this, SLOT("Fire()"));

So we need to keep a 'strong reference' (is that the correct term?) somewhere 
that will prevent the QKeySequence from being GC'd. Then when the parent 
GameBoard is deleted we need to remove the strong reference so that the 
QKeySequence can then be GCd ok.

One possibility that I was thinking of this morning is to keep a strong 
reference, and not a weak reference in the pointerMap in SmokeMarshallers 
that maps C++ instances onto C# ones. We already have code to clear the entry 
out of the table when a C++ instance is deleted, and so we would just need to 
add code to decide whether a strong or weak ref is needed when the item was 
inserted. Do weak and strong references have a common super class that we can 
use to define the dictionary - I haven't looked it up yet.

// The key is an IntPtr corresponding to the address of the C++ instance,
// and the value is a WeakReference to the C# instance.

static private Dictionary<IntPtr, WeakReference> pointerMap
     = new Dictionary<IntPtr, WeakReference>();

Other classes in Qt also act as container objects, like QTableWidget and we 
could do the same thing for the QTableWidgetItems that it contains.

How were the other things on the TODO list - does my description of 
Q_PROPERTYs make sense? That would be a good one to start with. Then we could 
set properties across dbus which would be very nice.

-- Richard



More information about the Kde-bindings mailing list