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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:38:56+00:00 2026-05-14T18:38:56+00:00

I used to be pretty sure the answer is no, as explained in Overriding

  • 0

I used to be pretty sure the answer is “no”, as explained in Overriding the Finalize method and Object.Finalize documentation.

However, while randomly browsing through FileStream in Reflector, I found that it can actually call just such a method from a finalizer:

private SafeFileHandle _handle;

~FileStream()
{
    if (this._handle != null)
    {
        this.Dispose(false);
    }
}

protected override void Dispose(bool disposing)
{
    try
    {
        ...
    }
    finally
    {
        if ((this._handle != null) && !this._handle.IsClosed)  // <=== HERE
        {
            this._handle.Dispose();   // <=== AND HERE
        }
        [...]
    }
}

I started wondering whether this will always work due to the exact way in which it’s written, and hence whether the “do not touch managed classes from finalizers” is just a guideline that can be broken given a good reason and the necessary knowledge to do it right.

I dug a bit deeper and found out that the worst that can happen when the “rule” is broken is that the managed object being accessed had already been finalized, or may be getting finalized in parallel on a separate thread. So if the SafeFileHandle’s finalizer didn’t do anything that would cause a subsequent call to Dispose fail then the above should be fine… right?

Question: so there might after all be situations in which a method on another managed class may be called reliably from a finalizer? I’ve always believed this to be false, but this code suggests that it’s possible and that there can be good enough reasons to do it.

Bonus: Observe that the SafeFileHandle will not even know it’s being called from a finalizer, since this is just a normal call to Dispose(). The base class, SafeHandle, actually has two private methods, InternalDispose and InternalFinalize, and in this case InternalDispose will be called. Isn’t this a problem? Why not?…

  • 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-14T18:38:57+00:00Added an answer on May 14, 2026 at 6:38 pm

    Yes, finalizers are allowed to call other methods. Hell, you can even do amusing stuff like re-registering types for finalizations, for instance. But you do have to explicitly check for null when dealing with finalizable instances, as finalizers are not guaranteed to run in any order.

    In that case it’s just a matter of closing things nicely as much as possible. If the handle has not been finalized yet, cool, let’s dispose it, otherwise, well, the finalizer did its best.

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

Sidebar

Related Questions

No related questions found

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.