Just Say No to Finalization!

I am working with some classes that wrap unmanaged APIs, so I have to be concerned with releasing the resources associated with these APIs – e.g., the IDisposable interface. I was a little confused about the distinction between Dispose() and Finalize(), and in my research I found this article by Brent Rector of Wintellect:

In a worst-case scenario, your object’s Finalize method won’t run until your process terminates gracefully. (Actually, the real worst case is that it never runs at all because the process terminates abnormally.) Shortly thereafter, non-memory resources will be released anyway, so the Finalize method served no real purpose other than to keep the application from running as fast as it otherwise could.

Just say No to Finalize methods!

It’s a good point. In a true worst case scenario – unhandled exception time – you won’t get any benefit from Finalize. So, given the (evidently) large performance penalty of Finalize, why bother? I tend to agree. However, the best practice according to Microsoft is, if you implement IDispose, you should also implement a Finalizer:

<Private _IsDisposed as Boolean = False 
''' <summary> 
''' public Dispose method intended for client use 
''' </summary> 
Public Overloads Sub Dispose() Implements IDisposable.Dispose 
Dispose(False) 
GC.SuppressFinalize(Me) 
End Sub 
''' <summary> 
''' common Dispose method; can be called by client or the runtime 
''' </summary> 
Protected Overridable Overloads Sub Dispose(ByVal IsFinalizer As Boolean)
If Not _IsDisposed Then
If IsFinalizer Then 
'-- dispose unmanaged resources
End If 
'-- disposed managed resources 
End If 
_IsDisposed = True 
End Sub 
''' <summary> 
''' called by the runtime only, at garbage collection time 
''' this protects us if the client "forgets" to call myObject.Dispose() 
''' </summary> 
Protected Overrides Sub Finalize() 
Dispose(True) 
End Sub>

It’s all rather contradictory.

Jeff Atwood

Written by Jeff Atwood

Indoor enthusiast. Co-founder of Stack Overflow and Discourse. Disclaimer: I have no idea what I'm talking about. Let's be kind to each other. Find me https://infosec.exchange/@codinghorror

⏲️ Busy signing you up.

❗ Something's gone wrong. Please try again.

✅ Success! Check your inbox (and your spam folder, just in case).

Recent Posts

map of the United States via rgmii.org showing all 3,143 counties by rural (gold) / metro (grey) and population

Launching The Rural Guaranteed Minimum Income Initiative

It's been a year since I invited Americans to join us in a pledge to Share the American Dream: 1. Support organizations you feel are effectively helping those most in need across America right now. 2. Within the next five years, also contribute public dedications of time or

By Jeff Atwood ·
Comments
Let's Talk About The American Dream

Let's Talk About The American Dream

A few months ago I wrote about what it means to stay gold — to hold on to the best parts of ourselves, our communities, and the American Dream itself. But staying gold isn’t passive. It takes work. It takes action. It takes hard conversations that ask us to confront

By Jeff Atwood ·
Comments
Stay Gold, America

Stay Gold, America

We are at an unprecedented point in American history, and I'm concerned we may lose sight of the American Dream.

By Jeff Atwood ·
Comments
I’m feeling unlucky... 🎲   See All Posts