Question

I try to get to grasp with Ants Memory Profiler. The problem I have is that I dont have an easy application that has a memory leak.

I used the sample of Redgate (QueryBee), but it was to contreived for my taste. There has to be an easier app for that.

I tried to make one up but it is not working. It is not working means: I dont have a memory leak. I read about calling ShowDialog without disposing the called form would get me a memory leak, but thats not the case here.

I use VS2010 and .NET 4.0

I am especially interested in issues that are very common.

Here is what I have so far.Can you get me a memory leak?:

MainForm

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MemoryLeakTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SubForm formToCall = new SubForm();
            formToCall.ShowDialog();
        }
    }
}

Subform

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace MemoryLeakTest
{
    public partial class SubForm : Form
    {


        public SubForm()
        {
            InitializeComponent();
        }

        private void SubForm_Load(object sender, EventArgs e)
        {

            ArrayList persons = new ArrayList();

            for (int i = 0; i <= 50000; i++)
            {
                var person = new {
                    Name = "1 SchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorschSchorsch",
                    LastName = "KluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluniKluni", 
                    Age = 50,
                    City = "LondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondonLondon",
                    Zip = "223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301223012230122301", 
                    Index = i 

                };
                persons.Add(person);
            }

            dataGridView1.DataSource = persons;
        }

        private void SubForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //this.Dispose();
        }


    }
}
Was it helpful?

Solution

Add this to your SubForm

public void mouse_handler(object sender, MouseEventArgs e)
{

}

And change the MainForm to do this

private void button1_Click(object sender, EventArgs e)
{
     SubForm formToCall = new SubForm();
     this.MouseClick += new MouseEventHandler(formToCall.mouse_handler);
     formToCall.ShowDialog();
}

Now it doesn't matter if you .Dispose() the SubForm or not, you will still have a "leak". Your MainForm keeps a reference to the SubForms indefinitely, through its mouse event handler, which is basically just a list of who is to receive the events, since that handler is never de-registered.

Ants will help you track down this, but rather manually, it'll show you objects still being alive and referenced from the root, and you have to discover that these objects should not referenced anywhere. I believe Ants also can generate warnings/etc. when it finds objects that have been .Disposed(), but are still referenced somewhere.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top