Access Violation exception in PowerPoint after "undo"

Add-in Express™ Support Service
That's what is more important than anything else

Access Violation exception in PowerPoint after "undo"
I am experiencing an access violation exeption in PowerPoint if an "undo" call deletes a previously inserted shape that is currently selected 
Phil Paul




Posts: 6
Joined: 2017-04-03
Dear Add-In express Team,

I am experiencing a system.access.violation exception in the following Scenario and would really appreciate any hint after breaking my head on this and looking through all sort of posts for two days

The error can be reduced by a very simple example (as shown below):

I have the WindowSelectionChange event check work with the selected shapes. I check the selection type and if this is >=2 I access the selection.shaperange. This generally works fine.

But when I insert a shape via code and the user executes "undo" while the inserted shape is selected the program throws an access.vioalation exception. The reason is that the selection.type property is still on "2" (shapeRange selected) eventhough the "undo" deletes the selected shape so that actually nothing is selected after the undo call.

The really strange thing is that this ONLY happens if the inserted shape is a group, all other
shapeTypes (simple autoshapes, Charts, smart arts, Pictures etc.) can be deleted by the undo call and the correct selection type (0) is restored.


This simple example reproduces the issue


 
'// Started via keyboard shortcut
    Sub copyAndInsertAGroupShape() Handles AdxKeyboardShortcut1.Action

        '// Declare variables
        Dim w As DocumentWindow = PowerPointApp.ActiveWindow
        Dim sel As Selection = w.Selection
        Dim slRange As SlideRange = sel.SlideRange
        Dim sl As Slide = slRange.Item(1)
        Dim ss As Shapes = sl.Shapes

        '// The first shape on the slide must be a group for the error to show
        Dim s As Shape = ss.Item(1)

        '// Copy the shape and paste it on
        Dim tmpRange As ShapeRange = s.Duplicate
        Dim newShape As Shape = tmpRange.Item(1)

        '// I select the shape here but error also shows if shape is selected manually
        newShape.Select()

        '// Releasing ComObjects or not makes no difference for the error
        ReleaseComObject(newShape)
        ReleaseComObject(tmpRange)
        ReleaseComObject(ss)
        ReleaseComObject(sl)
        ReleaseComObject(slRange)
        ReleaseComObject(s)
        ReleaseComObject(sel)
        ReleaseComObject(w)

        '// resetting for test purposes so we can try multiple times
        eventCompleted = True
    End Sub



    Private eventCompleted As Boolean = False

    '// SelectionChange Event needs to check the selection type correctly
    Private Sub WindowSelectionChange(sender As Object, hostObj As Object) Handles AdxPowerPointAppEvents1.WindowSelectionChange

        '// just so you do not get into an infinite loop
        If Not eventCompleted Then Exit Sub
        eventCompleted = False

        Dim myWindow As DocumentWindow = Nothing
        Dim mySelection As Selection = Nothing
        myWindow = PowerPointApp.ActiveWindow
        mySelection = myWindow.Selection

        '// The problem starts here: clicking "undo" in PowerPoint deletes the shape,
        '// but the selection type still shows selection type = 2
        If mySelection.Type >= 2 Then
            Dim myShapeRange As ShapeRange = Nothing

            '// This line fails with an access.violation exception
            '// but ONLY if the shape is a group
            myShapeRange = mySelection.ShapeRange
            ReleaseComObject(myShapeRange)
        End If

        ReleaseComObject(mySelection)
        ReleaseComObject(myWindow)

        '// Event went well execute next time as well
        eventCompleted = True

    End Sub



What I have tried

> I tried different code combinations with and without releasing the com objects, also Setting them to nothing and forcing garbage Collection: no effect

> I checked the selection type also with pure VBA - also there the selection type after the undo is wrong

> I tried handling the access violation. This generally prevents the error from showing directly but after clicking around a little bit, PowerPoint will crash.

> I tried "selection.unselect" to execute after the undo call. This will also prevent the error from showing but creates a very bad user experience with the user selection being deselected by the add-in.


My question

> What is the best approach to deal with this issue?

One possibility would be to somehow check if selection.shaperange has a "bad pointer". Is this possible without actually throwing the access violation exception?

Best regards and thank you very much for any hint,
Phil
Posted 03 Apr, 2017 14:44:36 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Phil,

Nice job!

A minor thing that doesn't influence anything is: the hostObj parameter of the WindowSelectionChange event already contains a PowerPoint.Selection object.

I confirm the issue. It occurs in PowerPoint 2016. It doesn't occur in PowerPoint 2010 and 2013.

If you duplicate a shape using the code above, then, after you click Undo, no sizing handles aren't shown for the shape, PowerPoint 2010-2016 hides the Drawing Tools | Format tab, and the Selection object for the active window reports Selection.Type=ppSelectionNone. This is expected.

If you duplicate a group using the code above, then, after you click Undo, sizing handles aren't shown for the shape thus indicating that the shape isn't selected. Still, PowerPoint 2010-2013 continues to display the Drawing Tools | Format tab, while the PowerPoint.Selection object for the active window reports Selection.Type=ppSelectionShapes. Getting Selection.ShapeRange in this situation produces different results: in PowerPoint 2010-2013, the call succeeds, in PowerPoint 2016 it causes an AcessViolationException.

I'm going to create a VSTO add-in demonstrating the issue and post it on the Office development forum at https://social.msdn.microsoft.com/Forums/en-US/home?forum=officegeneral. I'll publish a link here when I do this.

Phil Paul writes:
> I tried "selection.unselect" to execute after the undo call. This will also prevent the error from showing but creates a very bad user experience with the user selection being deselected by the add-in.


I believe this may be a correct workaround. You need to check if you undo a duplicated group before deslecting.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Apr, 2017 04:48:14 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Posted 04 Apr, 2017 07:13:03 Top
Phil Paul




Posts: 6
Joined: 2017-04-03
Dear Andrei,

first of all thank you for looking into the topic. Thank you for posting the link.
I will be interested to see if a solution is found!

To me it is at least good news that the error is not popping up in PowerPoint 2010 and 2013.
I will create a workaround for PowerPoint 2016.

Do you know if it is possible to check if the selection.shaperange is corrupted? Something in the line of

if hasBadPointer(selection.shaperange) then ...


I am not aware if there is a way to check the state of the selection.shaperange without actually throwing the access.violation excection.

best regards,
Phil
Posted 04 Apr, 2017 07:48:38 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Phil,

I doubt that a solution will be found; the issue looks like a bug in PowerPoint 2016. If so, we will need to wait for them to fix it.

Phil Paul writes:
Do you know if it is possible to check if the selection.shaperange is corrupted?


The only way is to call Selection.ShapeRange and get that exception.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Apr, 2017 08:47:27 Top
Phil Paul




Posts: 6
Joined: 2017-04-03
Thank you very much Andrei for your response.

I'll go for a (bit more complex) workaround then.
Posted 04 Apr, 2017 09:04:23 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
You are welcome!

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 04 Apr, 2017 10:13:30 Top
Andrei Smolin


Add-in Express team


Posts: 14106
Joined: 2006-05-11
Hello Phil,

The issue is confirmed. I've published a description on a UserVoice site at https://powerpoint.uservoice.com/forums/288949-powerpoint-for-windows-desktop-application/suggestions/18866461-powerpoint-selection-causes-accessviolationexcepti. Please vote for it; according to their policy they'll review every "idea" that has 20 or more votes.

Regards from Belarus (GMT+3),

Andrei Smolin
Add-in Express Team Leader
Posted 07 Apr, 2017 03:31:35 Top
Phil Paul




Posts: 6
Joined: 2017-04-03
Hi Andrei,

thank you for this: I cast my vote... hope it gets picked up.

Phil
Posted 08 Apr, 2017 00:15:30 Top