Reputation: 47
First of all thanks and Pardon me if my English is not good. I tried a lot to get how to implement IDisposable Interface. As per pictures i got from net i implemented the interface. In my test project i tried a lot so if i'm loading some image to memory i could see IDisposible works as per my expectation.To see the memory(PF Usage) I used task manager.Here "test.bmp" is 5mb file
These are the three experiments i have done
Dim i As Integer = 0
While (i < 1000)
i = i + 1
Dim bmp As New Bitmap("D:\test.bmp")
End While
It killed my memory. Then i tried
Dim i As Integer = 0
While (i < 1000)
i = i + 1
Using bmp As New Bitmap("D:\test.bmp")
End Using
End While
PF was steady until the 1000. Means memory released propery.
then i tried
Dim i As Integer = 0
Dim dt As DateTime = DateTime.Now
While (i < 1000)
i = i + 1
Using oTestDispose As New clsTestDispose
End Using
End While
Public Class clsTestDispose
Implements IDisposable
Dim bmp As New Bitmap("D:\test.bmp")
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 unmanaged resources when explicitly called
Me.Dispose()
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
This code is telling "System.StackOverFlowException". Why the dispose is not clearing the image object inside it?
Upvotes: 0
Views: 893
Reputation: 156898
Because you are overriding the Dispose
method and calling it again inside with Me.Dispose
, this method runs recursively.
You could call Dispose
with `MyBase:
MyBase.Dispose()
This will call the Dispose
on the derived class, preventing it to loop and end in a StackOverflowException
.
Upvotes: 2