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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T06:54:51+00:00 2026-05-19T06:54:51+00:00

Is there a way to detect whether or not an object has called GC.SuppressFinalize?

  • 0

Is there a way to detect whether or not an object has called GC.SuppressFinalize?

I have an object that looks something like this (full-blown Dispose pattern elided for clarity):

public class ResourceWrapper {
    private readonly bool _ownsResource;
    private readonly UnmanagedResource _resource;

    public ResourceWrapper(UnmanagedResource resource, bool ownsResource) {
        _resource = resource;
        _ownsResource = ownsResource;
        if (!ownsResource)
            GC.SuppressFinalize(this);
    }
    ~ResourceWrapper() {
        if (_ownsResource)
            // clean up the unmanaged resource
    }
}

If the ownsResource constructor parameter is false, then the finalizer will have nothing to do — so it seems reasonable (if a bit quirky) to call GC.SuppressFinalize right from the constructor. However, because this behavior is quirky, I’m very tempted to note it in an XML doc comment… and if I’m tempted to comment it, then I ought to write a unit test for it.

But while System.GC has methods to set an object’s finalizability (SuppressFinalize, ReRegisterForFinalize), I don’t see any methods to get an object’s finalizability. Is there any way to query whether GC.SuppressFinalize has been called on a given instance, short of buying Typemock or writing my own CLR host?

  • 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-19T06:54:52+00:00Added an answer on May 19, 2026 at 6:54 am

    If you want to confirm that finalization has been suppressed if your object doesn’t own the resource, perhaps you could have the finalizer assert that it owns the resource? The test would have to do GC.Collect and GC.WaitForPendingFinalizers, but production code wouldn’t have anything extra except the assert (which could be omitted from the production build). One slight caveat with the assert: if the thread that creates the object dies between creating the object and setting the ownership status, the finalizer may run inappropriately.

    That having been said, I wonder whether it would be better to have an abstract ResourceWrapper type, with separate subtypes OwnedResourceWrapper and SharedResourceWrapper, that own or do not own the resource in question. Then the subtype that doesn’t own resources wouldn’t need to have a finalizer in the first place. Note that it may be useful for SharedResourceWrapper to implement IDisposable as a no-op.

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

Sidebar

Related Questions

Is there a way to detect whether IIS is enabled or not? I know
In Microsoft SQL Server, is there a way to detect whether a database has
Is there a way to detect that a DirectShow filtergraph has reached the end
Is there a way to detect whether Ctrl or Shift has been pressed during
It seems to me there is no way to detect whether a drag operation
Is there a way to detect, from within the finally clause, that an exception
Is there a way to detect whether there is anything left inside a boost
In Delphi 2009 and Windows API, is there a way to detect that a
How can I detect whether or not an Image View has a picture in
Is there a way to detect if a flash movie contains any sound or

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.