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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:07:16+00:00 2026-05-22T13:07:16+00:00

We have a third party library that internally uses a SafeHandle to an unmanaged

  • 0

We have a third party library that internally uses a SafeHandle to an unmanaged resource. In some error cases it is necessary to dispose of the object and recreate it. But, there is a bug in the dispose implementation that prevents the Handle from being closed in a subset of these cases. Which prevents new objects from being successfully created until its finalizer runs.

Two solutions (both evil) have been proposed to deal with this until we can get the third party code fixed:

  1. Run a GC.Collect to get the finalizer to run and clean up the object

  2. Use reflection to get at the Handle and close it if dispose fails to do so

which of these is less evil and why?
Is there some other method that we haven’t considered that is less evil than either of these?

  • 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-22T13:07:16+00:00Added an answer on May 22, 2026 at 1:07 pm

    I’m in favour of private reflection. It’s a localized bug, so the solution should be local too. And it’s much clearer that what your code intends to do. And you probably can add some tests that notice once the bug has been fixed. So the hack can easily be removed once it’s not needed anymore.

    ...
    thirdPartyObject.Dispose();
    ThirdPartyDisposeBugWorkaround(thirdPartyObject);
    ...
    
    void ThirdPartyDisposeBugWorkaround(ThirdPartyClass thirdPartyObject)
    {
       //Do private reflection here
    }
    

    Forcing a GC on the other hand has a global effect. And there are many reasons(most of them bad) for interfering with the GC. It’s much less obvious what your code does. So the call might be kept even once the bug has been fixed.

    Old New Thing: Don’t use global state to manage a local problem

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

Sidebar

Related Questions

I have a third party library that internally constructs and uses the SqlConnection class.
Hi Everybody, I have a third party library that contains an error. When I
I have a Visual C++ 9 Win32 application that uses a third-party library. When
I have some third party library Foo with class FooBar . I think that
I have a third-party library that is sometimes throwing an exception. So I decided
I have a JNI library that interacts with a set of third party libraries,
I have a third party library that is available for download in either 32-bit
I have a third party library that I'm attempting to incorporate into a simulation.
I have a third-party library in my SVN repository and I'd like to associate
I have a third-party app that creates HTML-based reports that I need to display.

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.