Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old September 26th, 2006, 10:08 PM
Authorized User
 
Join Date: Sep 2006
Location: , , .
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to guntank
Default Dispose Pattern

My questions are related to my previous post at http://p2p.wrox.com/topic.asp?TOPIC_ID=50274

    class Parent : IDisposable
    {
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(Boolean disposing)
        {
            if (disposing)
            {
                Console.WriteLine("parent managed");
            }
            Console.WriteLine("parent unmanaged");
        }
        ~Parent()
        {
            Dispose(false);
            Console.WriteLine("parent dtor");
        }

    }
    class Child : Parent
    {
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                Console.WriteLine("child managed");
            }
            Console.WriteLine("child unmanaged");
            base.Dispose(disposing);
        }
        ~Child()
        {
            Dispose(false);
            Console.WriteLine("child dtor");
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Child c = new Child();
            c.Dispose();
            Console.WriteLine("end main");
        }
    }

In my understanding, when I call c.Dispose(), GC.SuppressFinalize(this) will suppress Child.Finalizer only. However, the output differs from what I hope -- both Parent.Finalizer and Child.Finalizer are suppressed. Please help me to digest it.

Note: Since Parent has implemented its Finalizer, Child.Finalizer must not be implemented to avoid twice disposing when we forget to call Child.Dispose(). I create Child.Finalizer here just to support this problem.




Similar Threads
Thread Thread Starter Forum Replies Last Post
Dispose() methods bpevangelista BOOK: Professional XNA Game Programming: For Xbox 360 and Windows ISBN: 978-0-470-12677-6 2 May 12th, 2007 11:15 PM
Dispose() pzmrcd .NET Framework 1.x 2 March 2nd, 2006 03:19 PM
Question : Dispose mike_abc Pro VB.NET 2002/2003 1 May 14th, 2005 02:17 PM
DirectoryInfo.GetFiles(pattern): search pattern fo arif_1947 VS.NET 2002/2003 1 October 19th, 2004 11:59 PM
dispose Ibn_Aziz C# 3 February 26th, 2004 05:17 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.