简体   繁体   English

GTK#如何正确清理小部件,内存泄漏(Glib.toggleref,Glib.signal)

[英]GTK# how to correctly clean widgets, memory leak (Glib.toggleref, Glib.signal)

I'm actually working on a software powered by GTK# 2.12, we have some memory leaks on it and I'm searching how to improve that. 我实际上正在开发由GTK#2.12驱动的软件,我们在上面存在一些内存泄漏,我正在寻找如何改进它。

I made a small app to test the correct way to get rid of a widget. 我制作了一个小应用程序,以测试摆脱小部件的正确方法。 I'm creating 10 000 Labels and then I delete them. 我正在创建10000个标签,然后将其删除。

Here's my Label class: 这是我的Label类:

namespace test_gtk_objects
{

    public class TimeLabel : Label
    {

        private bool disposed = false;

        protected DateTime _time;



        public TimeLabel(DateTime time)
        {
            Time = time;
        }


        ~TimeLabel()
        {
            Dispose(false);

            Console.WriteLine("Called dispose(false) of class" + this.ToString());
            GC.SuppressFinalize(this);
        }

        //dispose
        public override void Dispose()
        {
           // Console.WriteLine("Called dispose(true) of class" + this.ToString());
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
                return;

            if (disposing)
            {
                Hide();
                Unrealize();
                Unparent();
                Unmap();
                Destroy();
                Unref();
                Console.WriteLine("ref: " + RefCount.ToString()); 

                // Free any other managed objects here.
                //
            }
            disposed = true;
            base.Dispose();
        }

        protected void display(Label input_label)
        {
            input_label.Text = _time.ToString("HH:mm:ss");
        }


        internal DateTime Time
        {
            get { return _time; }
            set
            {
                if (_time != value)
                {
                    _time = value;
                    display(this);
                }
            }

        }
    }
}

as you can see I have overridden the dispose method as suggested. 如您所见,我已经按照建议重写了dispose方法。 I also tried some way to free most of the widget (especially with the unref() ) to be sure the widget is not linked to something else. 我还尝试了一种释放大部分小部件的方法(尤其是使用unref()),以确保该小部件未链接到其他东西。

In my main window, I just create thoses labels in à list and remove them by calling the Dispose method on each of them. 在我的主窗口中,我只是在àlist中创建这些标签,然后通过在每个标签上调用Dispose方法将其删除。 All my widgets seem to be deleted, but I always have some Glib. 我所有的小部件似乎都已删除,但是我总是有一些Glib。 toggleref , Glib. togglerefGlib。 Signal as you can see in my memory snapshot: 如您在我的内存快照中看到的那样发出信号

Memory snapshot 内存快照

I don't have an idea how I can get a rid on it if you can help me with that problem it will be very appreciated 如果您可以帮助我解决该问题,我不知道该如何解决,将不胜感激

David 大卫

I finally edited GTK sharp library to remove toggleref when I destroy a widget by calling Glib Dispose in the destroy method. 最终,当我通过在destroy方法中调用Glib Dispose销毁小部件时,我最终编辑了GTK Sharp库以删除toggleref。 My objects count doesn't grow up fast and all my objects are deleted when needed ! 我的对象数量并没有快速增长,并且在需要时删除了我所有的对象!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM