Click here to Skip to main content
15,896,154 members
Articles / Desktop Programming / Windows Forms

Finding Undisposed Objects

Rate me:
Please Sign up or sign in to vote.
4.93/5 (105 votes)
13 Aug 2009Ms-PL5 min read 199.4K   2.9K   235  
An application to find undisposed objects in your .NET application.
using System;
using System.Collections.Generic;
using System.Text;

namespace Leaky
{
    class LeakCreator
    {
        IntPtr val;

        ~LeakCreator()
        {
           System.Diagnostics.Debug.WriteLine("here");
        }
    }

    class Nasty
    {
        IntPtr val;

        ~Nasty()
        {
            System.Diagnostics.Debug.WriteLine("here");
        }
    }

    class ReallyNasty
    {
        IntPtr val;

        ~ReallyNasty()
        {
            System.Diagnostics.Debug.WriteLine("here");
        }
    }

    class AreYouNutsNasty
    {
        IntPtr val;

        ~AreYouNutsNasty()
        {
            System.Diagnostics.Debug.WriteLine("here");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            SubMethod();
            SubMethod2();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            SubMethod();
            SubMethod2();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();

            DeepNastyGenerate();
            DeepNastyGenerate2();

            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }

        private static void DeepNastyGenerate2()
        {
            for (int i = 0; i < 10; ++i)
            {
                Nasty nasty = new Nasty();
                ReallyNasty reallyNasty = new ReallyNasty();

                if (i % 2 == 0)
                {
                    AreYouNutsNasty aynNasty = new AreYouNutsNasty();
                }
            }
        }

        private static void DeepNastyGenerate()
        {
            for (int i = 0; i < 10; ++i)
            {
                Nasty nasty = new Nasty();
                ReallyNasty reallyNasty = new ReallyNasty();

                if (i % 2 == 0)
                {
                    AreYouNutsNasty aynNasty = new AreYouNutsNasty();
                }
            }
        }

        private static void SubMethod()
        {
            LeakCreator lc = new LeakCreator();
            Console.WriteLine(lc.ToString());
        }

        private static void SubMethod2()
        {
            LeakCreator lc = new LeakCreator();
            Console.WriteLine(lc.ToString());
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer Atmel R&D India Pvt. Ltd.
India India
I'm a 27 yrs old developer working with Atmel R&D India Pvt. Ltd., Chennai. I'm currently working in C# and C++, but I've done some Java programming as well. I was a Microsoft MVP in Visual C# from 2007 to 2009.

You can read My Blog here. I've also done some open source software - please visit my website to know more.

Comments and Discussions