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

  • Home
  • SEARCH
  • 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 3663924
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T01:35:09+00:00 2026-05-19T01:35:09+00:00

I have a question about how to use Dispose() and destructors. Reading some articles

  • 0

I have a question about how to use Dispose() and destructors. Reading some articles and the MSDN documentation, this seems to be the recommended way of implementing Dispose() and destructors.

But I have two questions about this implementation, that you can read below:

class Testing : IDisposable
{
    bool _disposed = false;

    protected virtual void Dispose(bool disposing)
    {
        if (!_disposed) // only dispose once!
        {
            if (disposing)
            {
                // Not in destructor, OK to reference other objects
            }
            // perform cleanup for this object
        }
        _disposed = true;
    }

    public void Dispose()
    {
        Dispose(true);

        // tell the GC not to finalize
        GC.SuppressFinalize(this);
    }

    ~Testing()
    {
        Dispose(false);
    }
}

GC.SupressFinalize(this) on Dispose()

When the programmer uses using or calls Dispose() explicity, our class is calling to GC.SupressFinalize(this). My question here is:

  • What this exactly means? Will the object be collected but without calling the destructor?. I guess that the anwswer is yes since destructors are converted by the framework to a Finalize() call, but I’m not sure.

Finalizing without a Dispose() call

Suppose that the GC is going to clean our object but the programmer did not call Dispose()

  • Why don’t we dispose resource at this point? In other words, why can’t we free resources on destructor?
  • What code must be executed in the if inside, and what outside?

    if (!_disposed) // only dispose once!
    {
       if (disposing)
       {
           //What should I do here and why?
       }
       // And what here and why?
    }
    

Thanks in advance

  • 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-19T01:35:09+00:00Added an answer on May 19, 2026 at 1:35 am

    1. What does SuppressFinalize do?

    It unregisters the object from the finalizer list, ie when the GC later collects the object it will ignore the presence of the destructor. This is a big gain in performance since the destructor would otherwise require the object’s collection, and that of everything it references, to be delayed.

    2. Why don’t we dispose [managed] resource at this point? In other words, why can’t we free [managed] resources on destructor?

    You could, but it is certain to be pointless: the object you’re in has become unreachable so all those owned managed resources are unreachable too. They will be Finalized and collected by the GC in the same run and calling Dispose() on them is unnecessary but not totally without risk or cost.

    2a What code must be executed in the if inside, and what outside?

    Inside the if(disposing), call _myField.Dispose()

    In other words, Dispose of managed resources (objects with a Dispose)

    Outside, call code to cleanup (close) unmanaged resources, like Win32API.Close(_myHandle).

    Note that when you don’t have unmanaged resources, as will usually be the case (look up SafeHandle), you don’t need a destructor and therefore no SuppressFinalize.

    And that makes that the complete (official) implementation of this pattern is only needed because of the possibility that Test is inherited from.
    Note that the Dispose(bool) is protected. When you declare your class Testing to be sealed, it is totally safe and conforming to omit the ~Testing().

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

Sidebar

Related Questions

I have a question about design winforms. Should I use, or not, group boxes
I have some questions about partitioning and strategy, how and when to use it.
I have a question about locking. This doesn't have to be only about record
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have figured out a lot about nhibernate, but this seems to be my
i have question about why should we use return in get,if dont use what
I have a question about the objective-C. I use the following code to add
I have a question about supporting of QtConcurrent in Qt for Symbian. I use
Hey guys, I have a question about Sphinx. I use Sphinx to index the

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.