Please find below a sample application which demonstrates the above problem.If we run this code from memory profiler we can see that List object live count is 1 even we close Form1.
//This is my Test form code
namespace ListTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
List<InfoClass> infoList = new List<InfoClass>();
//ArrayList infoList = new ArrayList();
InfoClass info1 = new InfoClass();
info1.Name = "John";
info1.Id = 1;
infoList.Add(info1);
InfoClass info2 = new InfoClass();
info2.Name = "John";
info2.Id = 1;
infoList.Add(info2);
ultraGrid1.DataSource = infoList;
}
}
}
//This is the infoclass code.Here i added IDisposible also but still the problem persists.
namespace ListTest
{
class InfoClass :IDisposable
{
private string _name = string.Empty;
private int _id = 0;
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
}
~InfoClass()
{
// Simply call Dispose(false).
Dispose (false);
}
public string Name
{
get
{
return _name;
}
set
{
_name = value;
}
}
public int Id
{
get
{
return _id;
}
set
{
_id = value;
}
}
}
}
Now from a mainform i am calling
Form1 form = new Form1();
form.Show();
So when i close Form1 ,with mainform(MDIParent still open) in memory profiler still i see the live count of infoClass[] as 1.
Here is the memory profiler results.
Namespace Name Total(Live instances) New(Live instances)
ListTest Form1 0 0 0 0 0 0 0 0 0.04 14.3
ListTest InfoClass 0 0 0 0 0 0 0 0 0.09 1.4
ListTest InfoClass[] 1 1 0 1 16 16 16 16 0.09 2.1
ListTest MDIParent 1 1 0 1 540 540 540 540 0.04 23.5
Please help me to fix this problem