Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:33:56+00:00 2026-05-25T17:33:56+00:00

I need some advice on the implementation of the Dispose method. In our application

  • 0

I need some advice on the implementation of the Dispose method.

In our application the user designs their own UI. I have a preview window that shows what the UI is going to look like. All object drawn in this UI ultimately derive from a common base class ScreenObject. My preview manager contain a single object reference to a ScreenGrid which is the grid object for the entire preview area.

Question #1

Some of my derived screen classes hold onto unmanaged resources, such as a database connection, bitmap image and a WebBrowser control. These classes need to dispose of these objects. I created a virtual Dispose method in the base ScreenObject base class and then implemented an override Dispose method in each of the derived classes that hold onto unmanaged resources. However, right now I just created a method called Dispose, I am not implementing IDisposable. Should I implement IDisposable? If so how do I implement it?

  • Just on the derived classes that have unmanaged resources
  • The base class and derived classes that have unmanaged resources OR
  • The base class and all derived classes including those that do not have unmanaged resources

Is it wrong to put a virtual Dispose method in a base class that doesn’t have unmanaged resources so that you can take advantage of polymorphism?

Question #2

In reading about the Dispose method and the IDisposable interface Microsoft states that the disposing object should only call the Dispose method for its parent. The parent will call it for its parent and so on. To me this seems backwards. I may want to dispose of a child but keep its parent around.

I would think it should be the other way around, an object being disposed should dispose of its children. The children should then dispose of their children and so on.

Am I wrong here or am I missing something?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:33:56+00:00Added an answer on May 25, 2026 at 5:33 pm

    Question 1: Implement IDisposable as well, using the following pattern:

    public class MyClass : IDisposable
    {
        bool disposed;
    
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    //dispose managed resources
                }
            }
            //dispose unmanaged resources
            disposed = true;
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
    

    Question 2: What Microsoft means is that a derived class calls dispose on it’s parent class. The owner of the instance only calls Dispose on the most derived type.

    An (shortened) example:

    class Parent : IDisposable 
    {
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    //dispose managed resources
                }
            }
            //dispose unmanaged resources
            disposed = true;
        }
    
    }
    class Child : Parent, IDisposable 
    { 
        protected override void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    //dispose managed resources
                }
                base.Dispose(disposing);
            }
            //dispose unmanaged resources
            disposed = true;
        }
    
    }
    class Owner:IDisposable
    {
        Child child = new Child();
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if(child!=null)
                    {
                        child.Dispose();
                    }
                }
            }
            //dispose unmanaged ressources
            disposed = true;
        }
    }
    

    The owner only calls Dispose on the Child, but not on the Parent. The Child is responsible for calling Dispose on the Parent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need some implementation advice. I have a MYSQL DB that will be written
Experts - I need some advice in the following scenario. I have a configuration
I have been playing with Compact Framework lately and i need some advice. I
I need some expect advice on how to handle the following:- I have a
I need some advice with the Java Print API. I have a program that
I need some advice on how I can change the implementation of some code.
Need some advice again please. This is regarding a single developer shop. I have
I need some advice as to how I easily can separate test runs for
I need some advice on what kind of pattern(s) I should use for pushing/pulling
I need some advice from experts :) I will develop a website using PHP

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.