Answered Disposing objects ?

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
I know I'll get chastised for my method here, but that is precisely why I come to this forum. I appreciate being corrected and I can't think of another way to do this ...

One form initiates another small form that is a very small window on top of the main form and is used as a very transient legend. The intention is for this legend to go away with ANY Keydown, Mouse Click or loss of Focus that occurs, whether it's focused on the legend OR the underlying form or any other way that the legend might lose focus. When I show the legend as a modal form, it doesn't lose focus when I click on the underlying form. The way I get this to work is by showing the Legend as a non-modal form, as in the following:
VB.NET:
Dim F As New KeyboardShortCuts

               ' Any code here for setup...

                F.Show()

Then in the Legend's Closing Sub, I include this:
VB.NET:
Private Sub KeyboardShortCuts_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

        If Not Me.Disposing Then Me.Dispose()
    End Sub

My question is: The variable "F" in the original calling procedure is never Disposed. I thought I was covering my bases by Disposing KeyboardShortCuts in it's own Closing Sub. Is it possible that "F" is still floating out there in memory as an active variable even tho I've Disposed the Form that it refers to...?
 
Last edited:
You need to understand the difference between a variable and an object. You don't dispose variables. You dispose objects. You could have 100 different variables all referring to the same object. How many times do you think you need to call Dispose?

That F variable only exists as long as the method it's declared in is executing. As soon as that method completes, that variable falls out of scope and no longer exists. The form still exists though, so you need to access it some other way in order to do anything to it. Using Me is just another reference to that same object.

The fact is that you're trying to solve a problem that doesn't exist. When you display a form by calling its Show method, it is implicitly disposed when it is closed. There's nothing extra for you to do.

What you probably ought to be doing is displaying this form as a modeless dialogue, i.e. an owned form. Like a modal dialogue, a modeless dialogue will remain over its caller all the time. Unlike a modal dialogue, a modeless dialogue will not prevent access to its caller. An example of a modeless dialogue is the Find & Replace dialogue in VS. It remains on top of the VS window but doesn't prevent you accessing it. It will also minimise, restore and close along with the VS window. To create a modeless dialogue, you simply change this:
VB.NET:
F.Show()
to this:
VB.NET:
F.Show(Me)
As I said, you can get rid of any code for disposing the form because it will happen on its own when you close it, either via code or user action.

You do need to dispose a modal dialogue but, as the code in the caller doesn't continue until it closes, you can always do that with a Using block.
 
Excellent JM !!! Thank You for clearing my head on that one !!!
After upgrading to VS2019, I now have 78 messages telling me all the things that I've screwed up. This was one of them... Telling me F was never disposed... There are many other messages (not errors per se) but warnings. Things like variables needlessly being assigned a value, even tho that variable is used in the next lines of code. Others are class level objects that get disposed in the Closing Sub that VS is saying never get disposed. It's hard to tell what I really need to deal with. Anyway, thank you !
 
Yeah, stuff like that is just alerting you to potential issues but the IDE can't analyse your code completely. When it says "F was never disposed" it doesn't really mean that you have to dispose a variable but really just uses that wording for brevity. It would be inaccurate to say that the object referred to by F is never disposed because it doesn't know that. An accurate wording of "the object referred to by F is not disposed via F" may be more confusing to people who aren't sure what's going on.
 
Back
Top