Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about removing objects from memory...

Status
Not open for further replies.

pzmgmb

Programmer
Apr 2, 2003
115
0
0
US
Consider this code:

Code:
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Dim frm As New Form1
        frm.Show()
    End Sub

When does this frm object get disposed? I'm pretty sure that frm gets slated for disposal when execution leaves the MyBase.Click sub.

Code:
    Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Click
        Dim frm As New Form1
        frm.ShowDialog()
	frm.Dispose
    End Sub

I understand this code stops all execution until it is closed so the Dispose method can be called right after it.

The reason i ask this is because i have an app that when i close the main form it still stays running in memory and i have to 'End Process' it.
I think there may be some things in memory that arent being cleared.

Any ideas why the program wont close?
 
this code:
Code:
Sub A
  Dim frm As New Form1
  frm.Show()
end sub

will start the new form in it's own thread.

This code:
Code:
Sub B
  Dim frm As New Form1
  frm.ShowDialog()
end sub

will start the form syncronously in the same thread (meaning the next line will not execute until the frm object has closed.

In sub B, frm is a local variable contained within the same scope. When the form closes, sub B will complete and frm go out of scope and await garbage collection.

In sub A, frm is a local variable, but it is contained by a different thread. After the form is opened Sub A will complete, and the frm variable will go out of scope. Even though the variable is out of scope, the memory still exists as the form is still open.

one option would be to make frm a class level variable in your main form. Then overload the dispose method of the main form to include a check to see if frm is open, and if it is, close it.

-Rick


VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
I'm not sure what you mean by overloading the dispose method.

Could you give me a snippet?
 
Form's actually already have an overloaded Dispose method. Open up the windows generated code region and look for the Dispose sub, add in the code to make sure the frm variable is closed.

Code:
  Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
    if not frm is nothing and also _
     frm.visible then
      frm.close
    end if

    If disposing Then
      If Not (components Is Nothing) Then
        components.Dispose()
      End If
    End If
    MyBase.Dispose(disposing)
  End Sub

-Rick

VB.Net Forum forum796 forum855 ASP.NET Forum
[monkey]I believe in killer coding ninja monkeys.[monkey]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top