<html><body><div style="color:#000; background-color:#fff; font-family:times new roman, new york, times, serif;font-size:12pt"><span class="tab">    <br></span><div><span class="tab">    Hi, Jordan,</span><br><span class="tab">    <br></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><span class="tab">    I checked your piece of code and reproduced the issue. Then I moved </span>GC.Collect(); after the AddTopLevelItem call and could not reproduce it any more. So it seems that the Add... methods do change ownership but the root was cleaned up by the GC because nobody owned it yet.</div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><span class="tab">    About your
 remark: I am predominantly a .NET programmer and I couldn't agree with you more. Unfortunately, you are also correct that it is very hard to implement. It is part of the ongoing work on Qyoto but it is really quite a task. </span><span class="tab">However, in this particular case I believe there may be a bug because the GC shouldn't touch the unmanaged code. That is, the reason your tree items disappear is that the underlying Qt objects are destroyed and thus the managed QTreeWidgetItem objects actually wrap nothing. I'll look into that tomorrow and in the weekend. Thanks for reporting and for your examples.</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><br><span class="tab"></span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style:
 normal;"><span class="tab">    Best regards,</span></div><div style="color: rgb(0, 0, 0); font-size: 16px; font-family: times new roman,new york,times,serif; background-color: transparent; font-style: normal;"><span class="tab">    Dimitar</span><span class="tab"><br></span></div><div><br></div>  <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font face="Arial" size="2"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Jordan Miner <jminer7@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> Dimitar Dobrev <dpldobrev@yahoo.com>; KDE bindings for other programming languages <kde-bindings@kde.org> <br> <b><span style="font-weight: bold;">Sent:</span></b> Thursday, February 14, 2013 9:58 PM<br> <b><span style="font-weight:
 bold;">Subject:</span></b> Re: [Kde-bindings] Qyoto memory problem<br> </font> </div> <br>
Hi Dimitar,<br><br>Thanks for looking at it. I tried using `new QTreeWidgetItem(root)`,<br>but it did not make a difference.<br><br>It might be a hard problem to solve, but ideally, Qt would act like a<br>.NET library. In a .NET library, you don't have to think about whether<br>an object has ownership of another, because as long as there is a<br>reference to the object, it won't get collected. All other .NET<br>libraries that I've used, including System.Windows.Forms, are fully<br>garbage collected (and you cannot write code with them where a<br>GC.Collect() can change behavior). In the code below, ideally, the<br>signal-slot connection would keep the wrapper object alive. Is this<br>difficult to implement?<br><br>class Test2 : QMainWindow<br>{<br>    class Wrapper : QObject<br>    {<br>        Action dg;<br>        public Wrapper(Action dg)<br>        {<br>   
         this.dg = dg;<br>        }<br>        [Q_SLOT]<br>        public void Invoke()<br>        {<br>            dg();<br>        }<br>    }<br><br>    public Test2()<br>    {<br>        QPushButton button = new QPushButton("Push Me!");<br><br>        var wrapper = new Wrapper(delegate<br>        {<br>            QMessageBox.Information(this, "Clicked", "You clicked the<br>button.", QMessageBox.StandardButton.Ok);<br>        });<br>        Connect(button, SIGNAL("clicked()"), wrapper, SLOT("Invoke()"));<br>        wrapper = null;<br>        GC.Collect();<br><br>        CentralWidget =
 button;<br>        Show();<br>    }<br><br>    [STAThread]<br>    public static int Main(String[] args)<br>    {<br>        new QApplication(args);<br>        new Test2();<br>        return QApplication.Exec();<br>    }<br>}<br><br>Thanks again,<br>Jordan<br><br>On Thu, Feb 14, 2013 at 9:57 AM, Dimitar Dobrev <<a ymailto="mailto:dpldobrev@yahoo.com" href="mailto:dpldobrev@yahoo.com">dpldobrev@yahoo.com</a>> wrote:<br>><br>>     Hello,<br>><br>>     The Qt docs are a bit vague about whether addChild and similar functions<br>> change the ownership of the passed object. However, in the chapter for<br>> QTreeWidgetItem they do give the following:<br>><br>> Items are usually constructed with a parent that is either a QTreeWidget<br>> (for top-level items) or a QTreeWidgetItem
 (for items on lower levels of the<br>> tree). For example, the following code constructs a top-level item to<br>> represent cities of the world, and adds a entry for Oslo as a child item:<br>><br>>      QTreeWidgetItem *cities = new QTreeWidgetItem(treeWidget);<br>>      cities->setText(0, tr("Cities"));<br>>      QTreeWidgetItem *osloItem = new QTreeWidgetItem(cities);<br>>      osloItem->setText(0, tr("Oslo"));<br>>      osloItem->setText(1, tr("Yes"));<br>><br>>     So you may try explicitly giving a parent to your items. I myself will<br>> test your example later today.<br>><br>>     Regards,<br>>     Dimitar Dobrev<br>><br>> ________________________________<br>> From: Jordan Miner <<a ymailto="mailto:jminer7@gmail.com" href="mailto:jminer7@gmail.com">jminer7@gmail.com</a>><br>>
 To: <a ymailto="mailto:kde-bindings@kde.org" href="mailto:kde-bindings@kde.org">kde-bindings@kde.org</a><br>> Sent: Thursday, February 14, 2013 5:37 PM<br>> Subject: [Kde-bindings] Qyoto memory problem<br>><br>> Hi,<br>><br>> I'm having a problem that seems like a bug in Qyoto. When I run the<br>> following code, usually (not always) I see a tree with two items under<br>> the root item. When I uncomment the GC.Collect(), one or both of the<br>> items no longer appear. It seems like the sub-items are getting<br>> garbage collected after they are added to the root item, but before<br>> the root item is added to the tree. The root item isn't keeping them<br>> from being collected like it should.<br>><br>> I also ran into another instance of an object being collected when it<br>> shouldn't be, but I'll post it later.<br>><br>> I'm currently using the .NET Framework (not Mono) on Windows XP.<br>><br>>
 using System;<br>> using System.Collections.Generic;<br>> using QtCore;<br>> using QtGui;<br>><br>> class Test1 : QMainWindow<br>> {<br>>     public Test1()<br>>     {<br>>         QTreeWidget treeWidget = new QTreeWidget();<br>>         QTreeWidgetItem root = new QTreeWidgetItem();<br>>         root.SetData(0, (int)Qt.ItemDataRole.DisplayRole, "Root Item");<br>><br>>         QTreeWidgetItem item = new QTreeWidgetItem();<br>>         item.SetData(0, (int)Qt.ItemDataRole.DisplayRole, "Item #1");<br>>         root.AddChild(item);<br>>         item = new QTreeWidgetItem();<br>>         item.SetData(0, (int)Qt.ItemDataRole.DisplayRole, "Item #2");<br>>         root.AddChild(item);<br>>     
    //GC.Collect();<br>>         treeWidget.AddTopLevelItem(root);<br>><br>>         CentralWidget = treeWidget;<br>>         Show();<br>>     }<br>><br>>     [STAThread]<br>>     public static int Main(String[] args)<br>>     {<br>>         new QApplication(args);<br>>         new Test1();<br>>         return QApplication.Exec();<br>>     }<br>> }<br>><br>> Thanks,<br>> Jordan<br>> _______________________________________________<br>> Kde-bindings mailing list<br>> <a ymailto="mailto:Kde-bindings@kde.org" href="mailto:Kde-bindings@kde.org">Kde-bindings@kde.org</a><br>> <a href="https://mail.kde.org/mailman/listinfo/kde-bindings"
 target="_blank">https://mail.kde.org/mailman/listinfo/kde-bindings</a><br>><br>><br>><br>> _______________________________________________<br>> Kde-bindings mailing list<br>> <a ymailto="mailto:Kde-bindings@kde.org" href="mailto:Kde-bindings@kde.org">Kde-bindings@kde.org</a><br>> <a href="https://mail.kde.org/mailman/listinfo/kde-bindings" target="_blank">https://mail.kde.org/mailman/listinfo/kde-bindings</a><br>><br><br><br> </div> </div>  </div></body></html>