0

Hey I am having an issue with an application that I have created. Basically for the first few weeks I am trying to step in to catch all first chance exceptions. This is fine but it is causing multiple instances of word to open and is causing my system to crash from out of memory. So my question is does anyone have an idea on how to best implement this or any suggestions on how to fix this?

enter image description here

And the code is pretty simple.

AppDomain currentDomain = AppDomain.CurrentDomain; 
currentDomain.FirstChanceException += LogException;




  public async Task LogException(string exception)
    {
        var word = new Microsoft.Office.Interop.Word.Application();
        var winVer = Environment.OSVersion.ToString();

        CiteRightException exceptionData = new CiteRightException();           
        exceptionData.stackTrace = exception;
        exceptionData.windowsVersion = word.Version;
        exceptionData.windowsVersion = winVer;            

        var content = new StringContent(Json.Encode(exceptionData), 
        Encoding.UTF8, "application/json");
        await httpClient.PostAsync("/api/logException", content);
    }
Seamy
  • 287
  • 4
  • 14
  • 2
    You need to learn how to dispose the Word instance. See https://stackoverflow.com/questions/6777422/disposing-of-microsoft-office-interop-word-application – Steve Oct 16 '17 at 14:20
  • That's the problem. Also, (if you can) you should ditch that ancient crap and just use the Open XML SDK https://msdn.microsoft.com/en-us/library/office/cc850833.aspx –  Oct 16 '17 at 14:22
  • 1
    Why do you need Word at all? It seems all you do is use its version number. Seems like you should only need to retrieve that once. – John Wu Oct 16 '17 at 14:25
  • And probably this is even more precise. https://stackoverflow.com/questions/25134024/clean-up-excel-interop-objects-with-idisposable/25135685#25135685 (it's excel but the rules applies also to word) – Steve Oct 16 '17 at 14:29
  • Need to support old ass versions of word... There's more going on here but this was the piece causing the issues. However looking at this. Will this not make my application close? What I am looking for is a way of catching first chance exceptions logging them and continuing on? – Seamy Oct 16 '17 at 14:35
  • Also the .quit method does not seem to work either. The application still continues creating instances but either way this is not really what I am looking for. – Seamy Oct 16 '17 at 15:10

0 Answers0