Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8467467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:39:14+00:00 2026-06-10T15:39:14+00:00

I read some articles and blogs on Implementation if IDisposable and GC working set.

  • 0

I read some articles and blogs on Implementation if IDisposable and GC working set. However, I could not understand the core areas of differentiation like:
Following is code of my test class:

Imports System.ComponentModel
Namespace Classes
    Public Class BaseClass
        Implements INotifyPropertyChanged
        Implements IDisposable

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

        Protected Friend Sub NotifyPropertyChanged(ByVal info As String)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        End Sub

#Region "IDisposable Support"
        Private disposedValue As Boolean ' To detect redundant calls

        Protected Overridable Sub Dispose(disposing As Boolean)
            If Not Me.disposedValue Then
                If disposing Then
                    ' TODO: dispose managed state (managed objects).
                End If
            End If
            Me.disposedValue = True
        End Sub
        Public Sub Dispose() Implements IDisposable.Dispose
            Dispose(True)
            GC.SuppressFinalize(Me)
        End Sub
#End Region

    End Class

    Public Class GenreClass
        Inherits BaseClass
#Region "Private Variables"
        Private _GenreValue As String
        Private _IconValue As String
        Private _IsSelectedValue As Boolean
        Private _IsExpandedValue As Boolean
#End Region

#Region "Property Variables"
        Property Genre As String
            Get
                Return _GenreValue
            End Get
            Set(Value As String)
                If Not _GenreValue = Value Then
                    _GenreValue = Value
                    NotifyPropertyChanged("Genre")
                End If
            End Set
        End Property
        Property Icon As String
            Get
                Return _IconValue
            End Get
            Set(Value As String)
                If Not _IconValue = Value Then
                    _IconValue = Value
                    NotifyPropertyChanged("Icon")
                End If
            End Set
        End Property
        Property IsSelected As Boolean
            Get
                Return _IsSelectedValue
            End Get
            Set(Value As Boolean)
                If Not _IsSelectedValue = Value Then
                    _IsSelectedValue = Value
                    NotifyPropertyChanged("IsSelected")
                End If
            End Set
        End Property
        Property IsExpanded As Boolean
            Get
                Return _IsExpandedValue
            End Get
            Set(Value As Boolean)
                If Not _IsExpandedValue = Value Then
                    _IsExpandedValue = Value
                    NotifyPropertyChanged("IsExpanded")
                End If
            End Set
        End Property
#End Region

        Protected Overrides Sub Dispose(disposing As Boolean)
            Genre = Nothing
            MyBase.Dispose(disposing)
        End Sub

        Public Overrides Function ToString() As String
            Return Genre
        End Function            
    End Class
End Namespace

My Test scenarios are as follows:
Test1:

Dim list1 As New List(Of HODLib.Classes.GenreClass)
For i = 0 To 4
    Using z As New HODLib.Classes.GenreClass

        With z
            .Genre = "asdasd"
            .Icon = "asdasdasdasdasd"
            .IsExpanded = True
            .IsSelected = True

        End With
        list1.Add(z)

    End Using
Next
For Each z In list1
    MessageBox.Show(z.ToString)
Next

result of test1 is that GC is called immediately and I loose access to resources, I get null message.

Test2:

Dim list1 As New List(Of HODLib.Classes.GenreClass)
For i = 0 To 4

    Dim z As New HODLib.Classes.GenreClass
    With z
        .Genre = "asdasd"
        .Icon = "asdasdasdasdasd"
        .IsExpanded = True
        .IsSelected = True

    End With
    list1.Add(z)

Next
For Each z In list1
    MessageBox.Show(z.ToString)
Next

Result is Dispose is never called, even for z in forloop, I dont understand this, why is z not disposed, is it waiting because the list has reference to its values?

Test3:

    Dim list1 As New List(Of HODLib.Classes.GenreClass)
        For i = 0 To 4

            Dim z As New HODLib.Classes.GenreClass
            With z
                .Genre = "asdasd"
                .Icon = "asdasdasdasdasd"
                .IsExpanded = True
                .IsSelected = True

            End With
            list1.Add(z)
            z.Dispose()

        Next
        For Each z In list1
            MessageBox.Show(z.ToString)
        Next

Result: Test2 vs test3 is calling the dispose manually after adding to the list. Result is that I lost access to resource, I get null message. Why did this happen although I added the object to list before calling the dispose method.

Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:39:15+00:00Added an answer on June 10, 2026 at 3:39 pm

    You are adding a reference to the instance z which you create. When that instance is destroyed, the reference is no longer pointing to anything. The Using construct automatically calls Dispose for you. You can make a new copy of the object z by adding a method to the class:

    Public Function Clone() As GenreClass
        Return DirectCast(Me.MemberwiseClone(), GenreClass)
    End Function
    

    and use list1.Add(z.Clone).

    See Object.MemberwiseClone Method for more information and if you need to create a deep copy.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've read some articles on the Internet that this is not possible. To communicate
I've read some of the articles on MSDN but they're not quite what I
I've read on some blogs and in some articles related to optimization, how to
I read some articles about how to set up eclipse and maven to create
I read some articles but really didn't understand what does select 1 from do?
I've read some articles saying you should set the cost to be at least
I've read some blog articles about Observer pattern implementation on JEE6 and something bother
I read some articles about Comet tech. All of them mentioned that the long-life
I've read some articles about Android NDK. Most of them claim Native C is
So I've read some articles about scaling Socket.IO. For various reasons I don't want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.