GTK# - 内存泄漏

时间:2015-06-30 11:38:49

标签: c# memory-leaks gtk gtk3

我使用GTK#for Windows创建了一个简单的gtk应用程序(Gtk#2.99.3)。

在视图创建和删除期间,我发现内存泄漏。所以我写了一个简单的应用程序来验证这一点:

        private static void MemoryTest()
    {
        //Create the view
        Gtk.Builder builder = new Gtk.Builder(null, "Modules.Dashboard.Dashboard.ui", null);


        //Create the parameters
        object[] parameters = new object[3] { builder, builder.GetObject("window1").Handle, 0 };
        //Load the current view
        var view = (ViewBase)Activator.CreateInstance(typeof(DashboardView), parameters);


        //TRY TO DESTROY;
        view.Destroy();
        builder.Dispose();

        view.Dispose();
    }

该函数被调用10000次:

        for(int i = 0; i < 10000; i++)
            MemoryTest();
        Console.WriteLine("Run GC");
        System.GC.Collect(GC.MaxGeneration, GCCollectionMode.Default, true);

        Console.WriteLine("Finsih GC");

        Thread.Sleep(TimeSpan.FromSeconds(30));
        for(int i = 0; i < 10000; i++)
            MemoryTest();

在执行此应用程序期间,所需的内存不断增加。

删除整个窗口需要进一步的步骤吗?

我的.ui文件:

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkGrid" id="view">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="row_homogeneous">True</property>
        <property name="column_homogeneous">True</property>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Ich</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">bin</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">0</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label3">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">ein</property>
          </object>
          <packing>
            <property name="left_attach">0</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label4">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">Dashboard</property>
          </object>
          <packing>
            <property name="left_attach">1</property>
            <property name="top_attach">1</property>
            <property name="width">1</property>
            <property name="height">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

0 个答案:

没有答案