Matt Fitzmaurice
Matt Fitzmaurice

Reputation: 1416

VSTO Add-in causing Word to crash on application close

I have created numerous VSTO Add-Ins over the last few years. They are running against many versions of MS Word (but mostly MS Word 2016). I share a common library of code that I add to when working on each new project.

I've noticed sporadic crashing when closing Word. It's the bad crash that requires task manager to clean up:

"Microsoft Word has stopped working" "Close the program?"

It happens very rarely. Rare enough that I shrug it off as "Crazy MS Word..".

My colleagues have also noticed the problem a number of times. Often it went away after I built them a new assembly (with little or no code changes..)

The situation is now more serious, seeing as a client has report the issue while testing. Interestingly, on the client machine the crashing is reproducible.

I've spent the last few days commenting out code in an attempt to identify the problem. I thought I had the problem isolated to some Ribbon Visibility code, however it turns out I was just going round in circles..

I've tried getting a crash dump via:

adsplus.exe -crash -pn winword.exe -o c:\Temp

After running this command I am unable to reproduce the error.

I noticed that changing my log4net tracing level from WARN to DEBUG caused the reproducible error to stop. I'm not confident that it's fixed however.

Is it a timing issue? Any idea how I can find the cause of my problem?

-- Edit --

@Thomas Weller, I was able to get a crash dump using https://learn.microsoft.com/en-us/sysinternals/downloads/procdump Will see how I go interpreting it.

Upvotes: 0

Views: 576

Answers (1)

Matt Fitzmaurice
Matt Fitzmaurice

Reputation: 1416

I was able to get a memory dump using ProcDump and it showed that a Thread exception was occurring as Word was closing.

Turns out I when I thought I was using the same dispatcher object, I was creating a new one each time.

I had code similar to:

Dispatcher dispatcher = System.Windows.Application.Current.Dispatcher;
O.CustomXMLParts parts = null;
dispatcher.WaitUntilApplicationIdle(() =>
{
    parts = doc.CustomXMLParts.SelectByNamespace(Office.Namespace);
});
...

public static void WaitUntilApplicationIdle(this Dispatcher dispatcher, Action action)
{
    dispatcher.Invoke(action, DispatcherPriority.ApplicationIdle);
}

And I had to ensure that the application was correctly set up as Word started:

void ThisAddIn_Startup(object sender, EventArgs e)
{
    EnsureApplication();
}

void EnsureApplication()
{
    if (System.Windows.Application.Current == null)
        new Application()
        {
            ShutdownMode = ShutdownMode.OnExplicitShutdown
        };
}

Upvotes: 1

Related Questions