Share via

Shapes memory leak using VBA

Anonymous
2015-12-29T01:53:05+00:00

I believe I have found a memory leak in using Shapes from VBA. I'm losing around 300k for every 30 or so shapes I create/delete from my worksheet. It also seems to reflect in the performance of my macro over time.

Microsoft 365 and Office | Excel | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Anonymous
    2016-01-03T01:08:43+00:00

    Thanks for the example.

    With it I can reproduce the problem (in 2010 and in 2013 on Windows 7) and so I will pass it on to Microsoft.

    If it is causing you a serious problem then you should also report it to Microsoft as a support incident.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2016-01-02T15:40:51+00:00

    This should do it. After doing some testing it seems to be specifically related to using 'pline.Placement = xlFreeFloating'. I have a specific reason for using it but I can probably live without it in the general case. I do realize that this isn't really an intended use case, the entirety of this project is pretty much completely out of the realm of every day Excel use.

    Private running As Boolean

    Sub main()

        running = True

        While running = True

                render

        Wend

    End Sub

    Public Sub render()

        Dim shp As Shape

        Dim pline As Shape

        Dim i As Long

        Dim faceArray(1 To 5, 1 To 2) As Single

        For Each shp In Sheet1.Shapes

                shp.Delete

                Set shp = Nothing

            Next shp

        For i = 0 To 30

                    faceArray(1, 2) = 1

                    faceArray(1, 1) = 1

                    faceArray(2, 2) = 1

                    faceArray(2, 1) = 10

                    faceArray(3, 2) = 10

                    faceArray(3, 1) = 10

                    faceArray(4, 2) = 10

                    faceArray(4, 1) = 1

                    faceArray(5, 2) = faceArray(1, 2)

                    faceArray(5, 1) = faceArray(1, 1)

                    Set pline = Sheet1.Shapes.AddPolyline(faceArray)

                    pline.Placement = xlFreeFloating

                    Set pline = Nothing

        Next i

        DoEvents

    End Sub

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2016-01-02T14:50:34+00:00

    If you would like this to be reported to Microsoft, it would be helpful if you can provide steps to reproduce the problem.

    Was this answer helpful?

    0 comments No comments