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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T15:28:32+00:00 2026-05-11T15:28:32+00:00

Recently I needed to compare a suggested pattern for IDisposable and object finalization with

  • 0

Recently I needed to compare a suggested pattern for IDisposable and object finalization with the auto-generated one we which VS2005/VB.NET provide. We have used the auto-generated one a fair bit, but after looking it the two side by side I had a number of questions about the VB.NET implementation…

For reference, here is the IDE’s implementation:

Public Class Class1     Implements IDisposable      Private disposedValue As Boolean = False        ''// To detect redundant calls      ''// IDisposable     Protected Overridable Sub Dispose(ByVal disposing As Boolean)         If Not Me.disposedValue Then             If disposing Then                 ''// TODO: free managed resources when explicitly called             End If              ''// TODO: free shared unmanaged resources         End If         Me.disposedValue = True     End Sub  #Region ' IDisposable Support '     ''// This code added by Visual Basic to correctly implement the disposable pattern.     Public Sub Dispose() Implements IDisposable.Dispose         ''// Do not change this code.  Put cleanup code in Dispose(ByVal disposing As Boolean) above.         Dispose(True)         GC.SuppressFinalize(Me)     End Sub #End Region  End Class 

Questions:

  1. If Finalize() is called during GC without object.Dispose() being explicitly called first then disposing:=false and the code within ‘if disposing…’ will never execute to free the managed resources–resulting in them remaining in memory until the next GC pass. Why wouldn’t these be explicitly freed? Wouldn’t doing so free more memory on the first GC pass and not leave unneeded objects in memory until the next pass?
  2. Why doesn’t the IDE generate the Dispose(false) call when overriding Finalize() on an IDisposable class?
  3. How would the GC know to call Dispose(false) and ensure that it is the IDE’s implementation and not a custom implementation which uses the bool parameter in a different manner? * …and shouldn’t Dispose(disposing as bool) be an interface member if the GC tests for its existence and uses it in a manner that assumes a certain implementation (object.Dispose(disposing:=false))? * In the presence of both Dispose() and Dispose(disposing as boolean) why would the GC ever opt to call the overloaded, non-interface member?

Overall I am confused by the supposed added value of having an extended code-path that executes when Dispose() is called explicitly(as opposed to having a common path that is executed regardless of whether or not Dispose() was called explicitly). While I can appreciate that it is provided with good intentions I can’t see how it does anything other than delay the actual release of managed resources if Dispose() isn’t called directly. In essence it seems to only work to make the managed resources unreachable in the object graph, orphaning them until the 2nd GC run rather than freeing them at a point where they are known to be no longer needed.

  • 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. 2026-05-11T15:28:32+00:00Added an answer on May 11, 2026 at 3:28 pm

    Your question has a logical error…if Dispose() is called within the Finalizer, then yes, disposedValue will be false, which means that If Not Me.disposedValue Then... will execute. The parameter passed for disposing is true, so all of the code within there should execute just fine.

    Edit (turns out the Finalizer calls Dispose(false))

    The Finalizer on the form (which only runs if Dispose() is never called on the Form) invokes Dispose(false). The reason for this is that the Form is currently being GC’ed. As a result, MANAGED resources (ie, components on the Form) will get collected and their own Finalizers should invoke Dispose() if required. Only unmanaged resources should be released in Dispose(false).

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

Sidebar

Ask A Question

Stats

  • Questions 94k
  • Answers 94k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As always, I figure out the answer only after giving… May 11, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer Beginning Objective C for the C# guy May 11, 2026 at 6:51 pm
  • Editorial Team
    Editorial Team added an answer Why is that error coming in? Because you're sending isEqualToString:… May 11, 2026 at 6:51 pm

Related Questions

I've been preparing to deliver a presentation on SQL Server Compact Edition 3.5 (SP1
I got into a mini-argument with my boss recently regarding project failure. After three
I recently had to struggle with one installation project (which uses most popular product
My current view is no, prefer Transact SQL stored procedures because they are a
The man page did not clearly specify this. But looking at openssl's apps implementations,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.