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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:28:53+00:00 2026-05-23T22:28:53+00:00

MSDN’s example pattern for implementing a Dispose() method depicts setting the reference to a

  • 0

MSDN’s example pattern for implementing a Dispose() method depicts setting the reference to a disposed managed resource to null (_resource = null), but does so outside the if (disposing) block:

protected virtual void Dispose(bool disposing)
{
    // If you need thread safety, use a lock around these 
    // operations, as well as in your methods that use the resource.
    if (!_disposed)
    {
        if (disposing) {
            if (_resource != null)
                _resource.Dispose();
                Console.WriteLine("Object disposed.");
        }

        // Indicate that the instance has been disposed.
        _resource = null;
        _disposed = true;   
    }
}

Shouldn’t _resource = null be placed inside this code block? If a call to Dispose(false) is made then _resource will be null and unable to be subsequently disposed! ??

Of course, Dispose(false) is only called (in practice) by the runtime during finalization. But if _resource wasn’t previously disposed, what is the need to set it to null at this point when the object (including the _resource member field) is about to be garbage collected?


[end of original question]

Follow up:

After much reading, it appears setting the reference to null is not necessary, but may be a good idea for "heavy" member objects if you have reason to believe the containing class (the one being disposed) might not be garbage collected soon.

Know that object disposal is no assurance that the object has been "released" by consuming code. The disposed object might be kept around (in a collection or otherwise) for various purposes, or just in error. I can imagine an application that uses objects from a collection then disposes of them, but keeps them in the collection for a later process to perform removal and log final state (or something like that… who knows…)

Conclusions:

  1. Setting references to "heavy" member objects to null releases them
    for garbage collection even if the disposed object is not released.
  2. It is overkill to clear references for all objects.
  3. Hence, placement of the _resource = null statement (the original question) is not important for two reasons: (A) Using it at all is only something to think about after reading the above; (B) In the MSDN example, it executes for both Dispose(true) and Dispose(false), but the latter only occurs when the object is finalized and just about to be garbage collected anyway!

Thus, my preference will be to place _resource = null inside the most inner if block:

if (disposing) {
    if (_resource != null) {
        _resource.Dispose();
        _resource = null;
    }
}

This keeps all the _resource code together. Further thoughts, anyone?

More reading:

  • In the Dispose(bool) method implementation, Shouldn't one set members to null?
  • Do you need to dispose of objects and set them to null?
  • http://www.codeproject.com/KB/dotnet/idisposable.aspx (Very good, and long!)
  • 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-23T22:28:54+00:00Added an answer on May 23, 2026 at 10:28 pm

    Setting it to null is removing the reference or pointer to the location in the heap. This lets the GC go through and remove anything that has no references to it without it having to do much guesswork. _resource would be some internal use object that needs to have its references cleaned up so lets say you have a Socket internal to your close you would close/dispose of that socket or any other persistent resource. Once it’s disposed you set it to null and remove all references (should remove all references) so that GC can do it’s job fine. The 2nd half of my answer is an example so it kind of repeats some things but I hope you get the idea.

    Setting it to null twice isn’t a big deal as there’s no effect. Disposing should be true at the time of cleaning up any resources and it’s only false once it is (as you said) about to be garbage collected anyway.

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

Sidebar

Related Questions

MSDN displays the following for CreatePatternBrush: You can delete a pattern brush without affecting
The MSDN documentation on Object.GetHashCode() describes 3 contradicting rules for how the method should
The MSDN states that the method returns true if the method is successfully queued;
MSDN provides a digit placeholder example: 1234.5678 (#####) -> 1235 I found this confusing
MSDN states that String.Intern retrieves the system's reference to the specified String and String.IsInterned
MSDN categorizes var under Types . variables that are declared at method scope can
From MSDN, I got an example as follows: IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator();
From MSDN : The system maintains a per-process reference count on all loaded modules.
MSDN defines System.Predicate this way : Represents the method that defines a set of
MSDN says that you should use structs when you need lightweight objects. Are there

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.