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.
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.
Here's my Label class:
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. 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.
In my main window, I just create thoses labels in à list and remove them by calling the Dispose method on each of them. All my widgets seem to be deleted, but I always have some Glib. toggleref, Glib. Signal as you can see in my 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. My objects count doesn't grow up fast and all my objects are deleted when needed !