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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:24+00:00 2026-05-25T19:20:24+00:00

I am debugging some C# class (let’s say, Foo) which has a Dispose-Finalize pattern

  • 0

I am debugging some C# class (let’s say, Foo) which has a Dispose-Finalize pattern implemented, i.e. its Finalizer is calling Dispose() in case if Dispose has not already been called.

Within the Dispose() there is logging code which accesses some member of Foo (let’s say Bar). Bar is also an instance of some reference type, is readonly (created in the Foo’s constructor) and is not exposed anywhere outside of Foo. So, at the time of Foo’s garbage collection Bar is likely to be already collected as well. The theory says that such member fields should not be accessed from the Finalize thread.

But the logging code was not aware of theory and tryed to log some of the Bar’s properties. And the process crashed with NullReferenceException in the Finalizer’s thread.

I understand that bad things may happen when you ignore the theory, but I was not expected NRE: does the Garbage Collector srts the references to collected objects to null? Or am I missing something else?

  • 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-25T19:20:25+00:00Added an answer on May 25, 2026 at 7:20 pm

    In Implementing a Dispose Method, they specifically caution about accessing other objects from the finalizer. That’s why they have the pattern:

    ~MyObject()
    {
        Dispose(false);  // False because it's being called in a finalizer
    }
    
    public void Dispose()
    {
        Dispose(true);  // True because it was called from user code
        GC.SuppressFinalize(this);
    }
    
    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // here it's safe to access other CLR objects
        }
    
        // Here you dispose of any unmanaged objects
    }
    

    If you depart from this pattern–especially if the finalizer calls Dispose and tries to access other objects that might already have been disposed, you’re going to have problems.

    Note also that you don’t need a finalizer unless you’re creating a class that allocates unmanaged resources. If your class is only using CLR objects or third party libraries that provide .NET objects, then there’s no need for the finalizer.

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

Sidebar

Related Questions

I've been tasked with debugging a Java (J2SE) application which after some period of
Here's the scenario. I'm debugging my own app (C/C++) which is using some library
I'd like to write some code which looks at a website and its assets
hi im trying to pass some values to a class but it wont let
I've been having trouble debugging some JavaScript which normally would seem easy to fix.
While debugging my code in eclipse IDE, the code enters into some class files.
Debugging some finance-related SQL code found a strange issue with numeric(24,8) mathematics precision. Running
I am debugging some code and have encountered the following SQL query (simplified version):
In Eclipse (Ganymede) I'm debugging some code that uses Apache Commons HttpClient and would
I'm very busy write now debugging some code, so I can't cookup a complete

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.