[Kde-bindings] Qyoto and QTreeView/QAbstractItemModel: "C# instance has been wrongly GC'd for virtual call"

Arno Rehn arno at arnorehn.de
Tue Nov 29 17:22:07 UTC 2011


On Monday 28 November 2011 09:27:50 Steven Boswell II wrote:
> On Friday 18 November 2011 12:06:34 Steven Boswell II wrote:
> >On Saturday, November 19, 2011 6:31 AM Arno Rehn wrote:
> >> It seems to me that anything passed to QTreeView.SetModel() should be
> >> accounted for within Mono's memory-management system, and not get
> >> garbage-collected behind its back.
> >
> >Yes, but that's a little difficult. The setModel() call will not take
> >ownership of the model, because it might be shared between multiple views
> >(see http://doc.qt.nokia.com/latest/qabstractitemview.html#setModel
> >for reference).
> 
> Is that relevant for C#?  Granted, I'm still learning C#, but I did a lot of
> work with garbage-collection back in my formative years, and it seems to me
> that QAbstractItemView should refer to its currently-set model with a
> pointer type that's relevant to C#'s garbage collection.  The advice given
> in the above link seems to be relevant for non-garbage-collected languages
> like C++.
Qyoto only wraps the C++ API, it doesn't reimplement Qt in C#. Consequently, 
it shares some of it's 'problems'.

<Bindings internals>
When you call SetModel() from C#, it only delegates that call to the C++ 
method with the unwrapped QAbstractItemModel as an argument.
Currently, we check in C#'s finalizer for C++ instances being 'owned' by other 
instances. That works fine for objects that, for example, have a parent set 
(such as most QObjects). If the instance is owned, we can garbage collect the 
C# wrapper, but keep the underlying C++ instance. Otherwise we get rid of 
both.
</Bindings internals>

QAbstractItemModel is a QObject, but its parent is not set by a call to 
setModel(). So the easiest thing to do is to simply give it a parent that 
lives at least as long as the view that's using the model.

-- 
Arno Rehn
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.kde.org/pipermail/kde-bindings/attachments/20111129/ff1855fc/attachment.html>


More information about the Kde-bindings mailing list