Memory leak?

M

milop

Hello. I have a Winform (3.51) application and am using my own
ApplicationContext.

When I dispose forms it appears as though the memory is not being
deacllocated.

I created a test app that exhibits this behvior( below).

In the app there are two forms (Form1 and Form2), with one button on each
for that calls RaiseContinueButtonPressed (so form1 is shown, then form2,
then form1, etc.). As I click each button the memory usage rises and never
falls, even though I am calling each form's Dispose() method.

Anyone have any idea why the memory is not being deallocated?

Thanks in advance,

Mike

'-------- Example Code ------------

'-------- Program.vb -------
Public NotInheritable Class Program
<STAThread()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
Application.Run(New MyApplicationContext)
End Sub
End Class

'------- MyApplicationContext class -------------
Public Class MyApplicationContext
Inherits ApplicationContext

Private _oldForm As Windows.Forms.Form
Private _currentForm As Windows.Forms.Form

Public Sub New()
AddHandler MyEvents.ContinueButtonClicked, AddressOf
HandleContinueButtonClicked
_currentForm = New Form1()
_currentForm.Show()
End Sub

Private Sub HandleContinueButtonClicked(ByRef sender As
Windows.Forms.Form)
_oldForm = _CurrentForm
_CurrentForm = New Form2()

_oldForm.Close()
_CurrentForm.Show()

End Sub
End Class

Public Class MyEvents
' User clicked the button on the form::
Public Shared Event ContinueButtonClicked(ByRef sender As
Windows.Forms.Form)
Public Shared Sub RaiseContinueButtonClicked(ByRef sender As
Windows.Forms.Form)
RaiseEvent ContinueButtonClicked(sender)
End Sub
End Class
 
M

milop

Oops, the code I posted was wrong.

Here's the right code:

Public Class MyApplicationContext
Inherits ApplicationContext

Private _oldForm As Windows.Forms.Form
Private _currentForm As Windows.Forms.Form


Public Sub New()
AddHandler MyEvents.ContinueButton1Clicked, AddressOf
HandleContinueButton1Clicked
AddHandler MyEvents.ContinueButton2Clicked, AddressOf
HandleContinueButton2Clicked

_currentForm = New Form1()
_currentForm.Show()

End Sub

Private Sub HandleContinueButton1Clicked(ByRef sender As
Windows.Forms.Form)
_oldForm = _currentForm
_currentForm = New Form2()

_oldForm.Dispose()
_currentForm.Show()

End Sub

Private Sub HandleContinueButton2Clicked(ByRef sender As
Windows.Forms.Form)
_oldForm = _currentForm
_currentForm = New Form1()

_oldForm.Close()
_currentForm.Show()

End Sub

Private Sub HandleCancelButtonClicked(ByRef sender As
Windows.Forms.Form)
_oldForm = _CurrentForm
_CurrentForm = New Form1()

_oldForm.Dispose()
_CurrentForm.Show()
End Sub

End Class

Public Class MyEvents
' User clicked the "Continue" button on form1:
Public Shared Event ContinueButton1Clicked(ByRef sender As
Windows.Forms.Form)
Public Shared Sub RaiseContinueButton1Clicked(ByRef sender As
Windows.Forms.Form)
RaiseEvent ContinueButton1Clicked(sender)
End Sub

' User clicked the "Continue" button on form2:
Public Shared Event ContinueButton2Clicked(ByRef sender As
Windows.Forms.Form)
Public Shared Sub RaiseContinueButton2Clicked(ByRef sender As
Windows.Forms.Form)
RaiseEvent ContinueButton2Clicked(sender)
End Sub
End Class
 
M

milop

Nevermind. I found the problem.

I was not removing the event handlers, so the GC wasn't cleaning up the
form.

I nice little gotcha.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top