1

I've recently moved over to Visual Studio 2019 V16.2 and it's now showing me a new "message" whenever I move between forms in my Windows Forms App.

IDE0067 Disposable object created by 'New FindFile' is never disposed

I've always moved to the "next" form in my project with code snippets like:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim frmFindFile As New FindFile
    frmFindFile.Show()
    Me.Close()

End Sub

What am I doing wrong? Should I be disposing of the form variable once the new one is showing? The below gets rid of the warning, but my second form never shows up!

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim frmFindFile As New FindFile
    frmFindFile.Show()
    Me.Close()
    frmFindFile.Dispose()
End Sub
Martin KS
  • 481
  • 7
  • 26
  • shouldnt it be `Me.Dispose` then? not sure if this is the correct way to do it though – AConfusedSimpleton Aug 02 '19 at 09:10
  • No - it's complaining about the new object created in the first line - frmFindFile – Martin KS Aug 02 '19 at 09:11
  • Ah right, didn't read it correctly. But you shouldn't dispose of the new form as long as you need it, only when you're done with it. – AConfusedSimpleton Aug 02 '19 at 09:15
  • Sure, and there are several places in the forms code where it closes itself - which should also dispose of it? – Martin KS Aug 02 '19 at 09:18
  • 1
    If you need the form again i would suggest to `hide()` it instead of closing it. If you don't need it again you can dispose it, but make sure no other forms hold a reference to it and might try to access it. I think the garbage collector will automatically dispose of it for you if there are no references to the object anymore. but i think someone more experienced can answer your question better than me tbh. – AConfusedSimpleton Aug 02 '19 at 09:25
  • 1
    VB.NET has default instances of forms, so if you just use `FindFile.Show()`, does it stop complaining? Also, FWIW, I don't see that warning in VS2019 Version 16.3.0 Preview 1.0. P.S. Remember to set [`Option Strict On`](https://stackoverflow.com/a/29985039/1115360) as the default for new projects as it comes set to Off. – Andrew Morton Aug 02 '19 at 10:55
  • Yes, that fixes it - somehow seems lazy, but it's obviously the prefferred way of doing it - ping it down as an answer and I'll mark it as correct :) – Martin KS Aug 02 '19 at 11:02
  • 1
    @MartinKS I'm not sure it's the *preferred* way of doing it, but I added a reference to further information in my answer for your perusal. – Andrew Morton Aug 02 '19 at 11:17

2 Answers2

3

VB.NET has default instances of forms, so if you just use FindFile.Show() it will not give a warning.

For more information, please see the answers at Why is there a default instance of every form in VB.Net but not in C#?

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84
1

I haven't seen the proper answer to this question yet, but there is one.

Original code as listed above is:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Dim frmFindFile As New FindFile
    frmFindFile.Show()
    Me.Close()

End Sub

Yet, the proper way to do this is:

Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    Using frmFindFile As FindFile = New FindFile()
        frmFindFile.Show()
    End Using
    Me.Close()

End Sub

This will automatically dispose of anything within the Using structure that requires disposal.

Pretty much anything that can be instantiated using the Dim/As class structure can be placed within a Using structure.

Another example would be using streamreader/writer. Instantiate it with the Using, then instead of using instance.Close(), just use End Using to close it out and dispose of it.

Andy
  • 27
  • 4