Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #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.


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 02: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 04:17 AM



All times are GMT -4. The time now is 02:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.