How to dispose of a particular instance of a class?
-
Hello, this might be a dummy question, but I couldn't find a good answer on the internet yet. In my code I populate a CheckedListBox with the filenames of a particular filetype. If one of the entries gets checked ('ItemCheck' event of the CheckedListBox), I create an instance of a class that represents one of the relevant files.
If ctrl.GetItemCheckState(ctrl.SelectedIndex) = 0 Then ' create a new cfgFile class AND add it to the classes list ff = New cfgFile(ctrl.SelectedItem) lbl\_FileMessage.Text &= ff.Message Else ' How to dispose of the correct instance? End if
How can I properly dispose of the correct classes' instance, once the item is unchecked again? I thought about looping through the instances and check for their 'filename' property... but there doesn't seem to be a collection of instances, which I can refer to in a loop. Thank you for your advice, Mick
-
Hello, this might be a dummy question, but I couldn't find a good answer on the internet yet. In my code I populate a CheckedListBox with the filenames of a particular filetype. If one of the entries gets checked ('ItemCheck' event of the CheckedListBox), I create an instance of a class that represents one of the relevant files.
If ctrl.GetItemCheckState(ctrl.SelectedIndex) = 0 Then ' create a new cfgFile class AND add it to the classes list ff = New cfgFile(ctrl.SelectedItem) lbl\_FileMessage.Text &= ff.Message Else ' How to dispose of the correct instance? End if
How can I properly dispose of the correct classes' instance, once the item is unchecked again? I thought about looping through the instances and check for their 'filename' property... but there doesn't seem to be a collection of instances, which I can refer to in a loop. Thank you for your advice, Mick
Where are you holding the created instances and how are they referenced to a particular line in the list box?
-
Where are you holding the created instances and how are they referenced to a particular line in the list box?
Hi Michael, I guess that's part of my question... I tried to put every newly created class into a List(of class), where I could add/remove entries. But removing elements from the list (i.e. list.count resulted in -1) didn't dispose of their instances (static counter variable, decremented inside the 'dispose' method, stayed the same). Thank you for guiding me further.
-
Where are you holding the created instances and how are they referenced to a particular line in the list box?
Would that be a probable solution?
Public Class cfgFiles
Inherits SortedList(Of String, cfgFile)Private Shared files As New SortedList(Of String, cfgFile) Public Overloads Shared Sub Add(ByVal fileName As String) files.Add(fileName, New cfgFile(fileName)) End Sub Public Overloads Shared Sub Remove(ByVal fileName As String) Dim ff As cfgFile = files(fileName) ff.Dispose() files.Remove(fileName) End Sub Public Overloads Function GetEnumerator() As IEnumerator(Of cfgFile) Return files.GetEnumerator End Function Public Property SelectedItem As cfgFile Default Public Overloads Property Item(index As Integer) Get SelectedItem = files(index) Return SelectedItem End Get Set(value) ' as cfgFile? files(index) = value End Set End Property
End Class
-
Would that be a probable solution?
Public Class cfgFiles
Inherits SortedList(Of String, cfgFile)Private Shared files As New SortedList(Of String, cfgFile) Public Overloads Shared Sub Add(ByVal fileName As String) files.Add(fileName, New cfgFile(fileName)) End Sub Public Overloads Shared Sub Remove(ByVal fileName As String) Dim ff As cfgFile = files(fileName) ff.Dispose() files.Remove(fileName) End Sub Public Overloads Function GetEnumerator() As IEnumerator(Of cfgFile) Return files.GetEnumerator End Function Public Property SelectedItem As cfgFile Default Public Overloads Property Item(index As Integer) Get SelectedItem = files(index) Return SelectedItem End Get Set(value) ' as cfgFile? files(index) = value End Set End Property
End Class
Memory allocated in .NET using the new command can automatically claimed by the GC as soon as the object loses scope. I say can, because it won't do it immediatly. Disposing is only required if there are unmanaged resources owned by the object. Which means you can safely remove the "ff.Dispose()" statement. Also see Object.Finalize Method (System)[^], Garbage Collection[^] and Implementing a Dispose Method[^].
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Memory allocated in .NET using the new command can automatically claimed by the GC as soon as the object loses scope. I say can, because it won't do it immediatly. Disposing is only required if there are unmanaged resources owned by the object. Which means you can safely remove the "ff.Dispose()" statement. Also see Object.Finalize Method (System)[^], Garbage Collection[^] and Implementing a Dispose Method[^].
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^][](X-Clacks-Overhead: GNU Terry Pratchett)
-
Thank you for this insight, Eddy. Still I am confused having the list.count variable reduced after removing the list entry (which is an instance), but the instance itself seems to be still alive (shared counter variable of the class doesn't decrement).