PowerPoint 2010: sporadic AccessViolationExceptions when setting shape/slide properties

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

PowerPoint 2010: sporadic AccessViolationExceptions when setting shape/slide properties
 
Robin Noack


Guest


Dear Add-in Express Team,

we have built an addin for PowerPoint with Add-in Express 2010 (6.1.3044). The addin seems to run fine with most versions of PowerPoint except for PP2010 (both 32/64 bit; we are not 100% sure though, that the issue does not occur on other versions), where we quite often receive AccessViolationExceptions when setting properties of shapes or slides, or when calling certain methods, e.g. slide.Export() or shape.Export(). We were not able to narrow down the error's source any further, because we can not reliably reproduce the error.

Can you give us any advise on how to avoid these exceptions or give us a hint on possible sources for this issue?

Thanks in advance,
Robin Noack
Posted 09 Nov, 2010 06:21:19 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hi Robin,

Can you show me how those methods are invoked in your code?

How do you deploy the interop assemblies you use in your project? Is the setup project generated by Add-in Express? If so, what modifications did you made in regard to interop assemblies?


Andrei Smolin
Add-in Express Team Leader
Posted 09 Nov, 2010 07:04:06 Top
Robin Noack


Guest


Hi Andrei,

thank you for your fast reply!

We use the Microsoft PowerPoint XP interop assemblies which ship with Add-in Express. These assemblies are deployed to the application (add-in) folder through a setup project that was created with Add-in Express. As far as I know, no modifications were made to the setup project regarding the interop assemblies.

Here is an example of one of our methods which invokes the .Export function and which is known to have thrown AccessViolationExceptions at the invoke:


public static void CreateThumbnails(this SlideRange slides, out byte[] thumbnail)
        {
            string tempPath = Path.ChangeExtension(Path.GetTempFileName(), ".png");

            // Create a thumbnail for the first slide only
            Slide slide = slides[1];

            // Write the thumbnail to a file
            // This call may throw AccessViolationExceptions
            slide.Export(tempPath, "PNG", 320, 240);

            //..more code, but the exception is thrown before..
        }


We also received AccessViolationExceptions when trying to set properties on shape objects. The setters are called at different places in our code. There is no obvious relation between the place in our code and the exceptions. What these exceptions seem to have in common, though, is that the shapes on which the properties are set have been pasted from the clipboard (I'm afraid we cannot completely verify this). Our logfiles indicate, that these exceptions are thrown most frequently at these properties:


System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at Microsoft.Office.Interop.PowerPoint.TextFrame.set_AutoSize(PpAutoSize AutoSize)
at ...

System.AccessViolationException: ...
at Microsoft.Office.Interop.PowerPoint.TextRange.set_Text(String Text)
at ...

System.AccessViolationException: ...
at Microsoft.Office.Interop.PowerPoint.Adjustments.set_Item(Int32 Index, Single Val)
at ...


Please let me know, if you need further information.

Thanks & regards,
Robin
Posted 09 Nov, 2010 08:00:10 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hi Robin,

Do you create threads in your code?

Robin Noack wrote:
Our logfiles indicate ...


Can you please send more detailed stack traces to the support e-mail address, see {Add-in Express}\readme.txt. Please make sure your e-mail contains a link to this topic.

And if it is only possible, please send me your add-in module; the actual business logics can be removed as I need to look at what you do with PowerPoint objects and events.


Andrei Smolin
Add-in Express Team Leader
Posted 09 Nov, 2010 08:21:36 Top
Robin Noack


Guest


Hi Andrei,

sorry for the delay, but it took us some time to set up a sample add-in and to reproduce the error on a customer's machine (at least once).

I sent an email to your support address with some more information and the sample code attached.

Do you create threads in your code?


We do create threads in our production addin, but we do not interact with the PowerPoint object model within these threads. While testing, we also logged the calling threads right before some of the method calls (on PPT objects) which possibly throw these errors, to confirm that they are only called from the main thread in all execution paths.

If you need any more information, please feel free to ask.

Thanks & regards,
Robin
Posted 19 Nov, 2010 14:12:26 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hi Robin,

I've got the project. Thank you.

I have a strong suspicion that the issue occurs due to a non-released COM object; you use a lot of COM objects and none of them gets released. I suggest that you transform your code so that every COM object is properly released. I know this may look like a serious code modification, nevertheless I know this for sure: releasing COM objects is what Office expects from you.

Here is an example:

oldShape.Fill.ForeColor.RGB = value;


That code should be transformed as follows:


using PP = Microsoft.Office.Interop.PowerPoint;
...
PP.FillFormat fill = oldShape.Fill;
PP.ColorFormat foreColor = fill.ForeColor;
foreColor.RGB = value;
Marshal.ReleaseComobject(forecolor); forecolor = null;
Marshal.ReleaseComobject(fill); fill = null;


That methodics as well as other useful (indeed) things relating to COM-NET interaction are descibed in http://www.add-in-express.com/creating-addins-blog/2008/10/30/releasing-office-objects-net/

Hope this helps.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Nov, 2010 08:36:54 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Oh, the code above is a raw sketch; I haven't tried to compile it.


Andrei Smolin
Add-in Express Team Leader
Posted 22 Nov, 2010 08:40:25 Top
Robin Noack


Guest


Hi Andrei,

thanks again for your quick response - we appreciate your support!

I read your (helpful and easy-to-understand) Q&A on releasing COM objects in .NET. Following your example and the guidelines in the Q&A, I restructured the test add-in so that each COM object should now be released after usage.

We ran another test on the client's machine, on which we are able to reproduce the error. The error (AccessViolationException) still occurs at the same place (when calling .Paste() in CreateRandomShapeCopy(), probably on the 10th to 20th iteration), no matter whether we release COM objects or not (can be toggled by a checkbox on the ribbon).

I sent the modified test add-in to your support e-mail address.

Thanks & regards,

Robin
Posted 23 Nov, 2010 10:32:02 Top
Andrei Smolin


Add-in Express team


Posts: 18817
Joined: 2006-05-11
Hi Robin,

Accessing a COM collection by index DOES create a COM reference; below is some code fragment demonstrating how this affects your code. Please check if modifying the code in this way helps.

COMHelper __CHO = new COMHelper();

PowerPoint.Presentations presentations = PowerPointApp.Presentations;
__CHO.Add(presentations);

while (presentations.Count < 2)
{
    Presentation presentation = presentations.Add(MsoTriState.msoTrue);
    __CHO.Add(presentation);
}

Presentation presentation1 = presentations[1];
Presentation presentation2 = presentations[2];

__CHO.Add(presentation1);
__CHO.Add(presentation2);

DoSomething(presentation1, presentation2);

// Release COM objects
__CHO.ReleaseObjects();



Andrei Smolin
Add-in Express Team Leader
Posted 24 Nov, 2010 09:41:47 Top
Robin Noack


Guest


Hi Andrei,

thanks for the correction! Indeed, I was mislead by an error while restructuring the code in the first place and drew the wrong conclusion. I eliminated the error and modified the code, so that references, which are created by accessing COM collections by index, are released too.

We are going to run another test with our client tomorrow - I'll post the results afterwards.

Regards,
Robin
Posted 24 Nov, 2010 10:44:10 Top