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 log them to the database and continue as normal. Now here's the sticker I do not want the application to close. I just want to log the error and continue, so app.quit etc is not what I am looking for. If it hit's the unhandled exception I have a seperate error handler for that. 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);
}

I have tried using a .quit method (Did not stop extra instances opening) and thinking it might be a thread issue currentThread.join() but still new instances are being created. If I comment out the firstchanceexception line everything works fine so it is 100% coming from here.

Thanks very much in advance

Seamy
  • 287
  • 4
  • 14
  • have you tried unsubscribing from it inside the `LogException` method start and subscribe again at last line ? – Ehsan Sajjad Oct 16 '17 at 16:51
  • what is the reason/purpose for your TAP pattern for this implementation? – DaniDev Oct 16 '17 at 17:01
  • "I just want to log the error and continue" Continue presumably means your application (?) you are instantiating Word just to retrieve the version. You could try to pass it as a constant param assuming your application is interacting with Word already. – DaniDev Oct 16 '17 at 18:13

1 Answers1

3

You must dispose all COM objects.

Please, see this Disposing of Microsoft.Office.Interop.Word.Application

In your case:

System.Runtime.InteropServices.Marshal.ReleaseComObject(word)

PS: Do it in a try .. finally block, just in case you have other exception.

bruno.almeida
  • 2,746
  • 1
  • 24
  • 32
  • 1
    Bruno is right. You need to dispose the word instances that you create to resolve the issue that you are experiencing, however, your entire approach for the error handling is incorrect. As a rule of thumb, you should NEVER return the stack trace to the client, because that reveals too much data about the coed and hackers can use it against you. Use a logging tool (such as NLog) and log the exception details on the server and pass a generic error message to the client. – Sparrow Oct 16 '17 at 17:02