I have a class that implements IDisposable because it has a private member field “foo” that is IDisposable (which is initialized in the constructor). I am unexpectedly getting a CA2000 Code Analysis error, which wants me to be sure to dispose of foo. However, I have foo.Dispose() in my class’s Dispose() code, which should take care of this.
I did some searching around and surprisingly can’t find an answer. What am I doing wrong? Clearly I missing something basic. How do I write my code to overcome this?
My VB code:
Public Class Bar
Implements IDisposable
Private Foo As SomeDisposableThing
Public Sub New()
Foo = New SomeDisposableThing() With {.name = "hello"}
End Sub
'''' snip ''''
Private disposedValue As Boolean = False ' To detect redundant calls '
Protected Overridable Sub Dispose(ByVal disposing As Boolean)
If Not Me.disposedValue Then
If disposing Then
If Foo IsNot Nothing Then Foo.Dispose()
End If
End If
Me.disposedValue = True
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
Dispose(True)
GC.SuppressFinalize(Me)
End Sub
End Class
The CA2000 error doesn’t refer to the container implementing
IDisposablebut rather the use of a local which is not properly disposed. The reason why is that you are using an object initializer on the disposable object. The actual code that will get generated is essentially the followingThis code is correctly flagged by FxCop as not properly disposing of an
IDisposablein all instances (it’s possible for an exception to occur on thetemp.Name = "hello"line in which case it wouldn’t be disposed).The fix is to not use an object initializer here and initialize
Foodirectly