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 9207695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T00:26:39+00:00 2026-06-18T00:26:39+00:00

First in this MSDN page there is a standard Dispose pattern. And there is

  • 0

First in this MSDN page

there is a standard Dispose pattern. And there is a bool as the parameter of the protected Dispose method to tell the GC whether managed resources are freed manually already, so that the GC does not need to care about them.

Now the question is, what exactly should be done inside the if (disposing) {} block? Normally the GC cleans up managed resources, so one doesn’t need to do anything special. But since inside this block, one needs to explicitly clean up managed resources, does that mean one just set all the fields and stuff in the object to null?

Second, isn’t it just nicer to have just one destructor (or finalizer whatever it calls) in the language? And then in the GC design, just put a bit to determine if the destructor is already called so that there is no need to garbage collect it, or the destructor is not yet called and the GC should clean it up. I found the Dispose pattern quite complicated, and I am very confused what to clean up in which function and how to clean up in derived classed. And by using a signle destructor design, the GC just cleans things up when they are not cleaned up yet, and does not clean up when they are already.

Greetings

PS: So is this also a good and simpler pattern to clean up objects?

class Foo
{
    bool unmanagedDisposed = false;
    void Dispose() {/*clean up unmanaged resources*/ unmanagedDisposed = true;}
    ~Foo() {if (!unmanagedDisposed) Dispose();}
}

So if the programmer knows and remembers to call Dispose(), nothing to do in the finalizer, otherwise clean up unmanaged resources in the finalizer. And here we don’t need to care about those managed resources.

  • 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-06-18T00:26:40+00:00Added an answer on June 18, 2026 at 12:26 am

    what exactly should be done inside the if (disposing) {} block?

    You clean up the managed resources, ie you call Dispose() on all IDisposable objects that you own at that point.

    to explicitly clean up managed resources, does that mean one just set all the fields and stuff in the object to null?

    No, it does not mean that. It’s only about IDisposable objects.

    isn’t it just nicer to have just one destructor (or finalizer whatever it calls) in the language?

    We only have one destructor aka Finalizer and Dispose() is not it. That makes the rest of your paragraph irrelevant.

    We have the Disposable pattern and the GC, they are related and cooperating but not the same. The GC manages memory and only memory. IDisposable is for managing resources (stream, connections, bitmaps).

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

Sidebar

Related Questions

This page (MSDN) says in the first step: Alternatively, you can also use configuration
I was just browsing msdn and found this page . I did never see
I have looked at this question first: retain value after page refresh . But
I'm making my first Windows Phone App As per this tutorial [ http://msdn.microsoft.com/en-us/library/gg680266(v=pandp.11).aspx ],
Code first: '''this is main structure of my program''' from twisted.web import http from
In linking a sports event to two teams, at first this seemed to make
This first bit works: $my_id = 617; $post_id_7 = get_post($my_id); $title = $post_id_7->post_excerpt; echo
(At first glance this may look like a duplicate of Different execution plan when
I've first observed this issue in a production code, then made a prototype: import
i have two arrays like this first array Array ( [0228] => Array (

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.