0

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.

Matt Fitzmaurice
  • 1,319
  • 4
  • 19
  • 40
  • 1
    Try [ProcDump](https://learn.microsoft.com/en-us/sysinternals/downloads/procdump) instead of ADplus – Thomas Weller Nov 28 '17 at 23:18
  • Don't multi-thread in VSTO. – Chris Feb 16 '18 at 20:08
  • Did you ever resolve this? I'm seeing similar errors after all my users upgraded to Word 2013. I ran into this while multithreading before (hence my assumption above, which appears to be incorrect, sorry) but I'm not doing that any more. I also see vague errors in the event log, not sure if they're related (`The process was terminated due to an unhandled exception.`). Looks like it may be related to ElementHost (Winforms/WPF interop), which I use in the actions pane. – Chris Jun 04 '18 at 19:51
  • Yeah, I've just added an answer below. Have you posted a question with all the details of your issue? If so, what's the link? – Matt Fitzmaurice Jun 06 '18 at 01:50
  • 1
    Thanks @MattFitzmaurice, I will check to see if I'm using dispatcher in the wrong way. I never posted my own issue as a question, I figured it out (or thought I had) before it came to that. It wasn't anything too complex, just calling `Exit` or `Close` inside another explicitly-created thread. – Chris Jun 18 '18 at 13:07

1 Answers1

1

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
        };
}
Matt Fitzmaurice
  • 1,319
  • 4
  • 19
  • 40