Tech Off Post

Single Post Permalink

View Thread: VB.NET Windows XP PowerPacks ShapeContainer disposing Error
  • User profile image
    wannymiarel​li

    Hello all,

    I am working on a project in VS2012 that should be compatbile with Windows XP.

    this is the code by alexkai

    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing Then DisposeShapeContainer(ShapeContainer1)
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
    Protected Sub DisposeShapeContainer(ByVal AShapeContainer As Microsoft.VisualBasic.PowerPacks.ShapeContainer)
        If AShapeContainer IsNot Nothing Then
            If AShapeContainer.Shapes IsNot Nothing Then
                Dim tshapes As New Generic.List(Of Microsoft.VisualBasic.PowerPacks.Shape)
                For Each tshape As Microsoft.VisualBasic.PowerPacks.Shape In AShapeContainer.Shapes
                    tshapes.Add(tshape)
                Next
                AShapeContainer.Shapes.Clear()
                AShapeContainer.Shapes.Dispose()
                For Each tshape As Microsoft.VisualBasic.PowerPacks.Shape In tshapes
                    tshape.Dispose()
                Next
            End If
            AShapeContainer.Dispose()
        End If
    End Sub

    this method is called by Form.Close, and it runs fine.
    after Form.Close is called by Form.Dispose and the method starts again, this generates an exception "Object reference not set to instance of an object" because it has been previously destroyed by Form.Close.

    To avoid this thing, my solution, maybe not one of the brightest, was to modify two lines of the method.
    The first, changing ByVal to ByRef (directly to refer to the object concerned and not to a copy)

    Protected Sub DisposeShapeContainer(ByRef AShapeContainer As Microsoft.VisualBasic.PowerPacks.ShapeContainer)


    and I added this at the end, after AShapeContainer.Dispose()

    AShapeContainer = nothing

    to mark the current object as Nothing, to exit from this is if the Object is set to Nothing

    If AShapeContainer IsNot Nothing Then

    If someone has a better solution would be nice to be able to show! I'm working on something more refined I will publish when ready.

    I'm sorry for my not "perfect" English.