-1

I've got a C# Windows Forms application that (outside of debug mode) works perfectly. When I run it in debug mode, each action I take has a random chance of crashing the application (like clicking a button, or closing the main form). There is a lot of async code running in the background, but the app crashes even when no tasks seem to be running (the _formClosing method is an entirely Synchronous method). Notably, the first thing most button clicks in my app do is change the visibile and enabled properties of some buttons, and then will start writing messages to the log - the app crashes before any logs get written (using NLog)

The error being thrown is:

System.Runtime.InteropServices.SEHException
  HResult=0x80004005
  Message=External component has thrown an exception.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

With output

An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in mscorlib.dll

and trying to resume the application just ends the debugging session.

The fact that it's not any one specific section of code or event that's triggering the crash, and that the code never crashes outside of debug mode leads me to believe this might be a Visual Studio issue. Are there any settings I can configure within Visual studio to make this crash less prone to occurring? or if not, is there any way to get more information than 'cannot Evaluate the stackTrace'?

Other Information that may or may not be relevant, the memory and CPU usage are both very low at the time of crashing (40MB or so and about 5% CPU utilisation). I'm running the latest Visual Studio Community 2019. The app runs on .Net Framework 4.8. My OS is a local VM running windows 10 enterprise. I also don't think I'm running any Trusteer applications.

My code is also all heavily Try Catched, especially around the button clicks.

Thanks for any help, let me know if I'm missing any relevant information

Edit: I've discovered the line that's causing the issue, commenting out this line causes the app to run fine, completely error free

var ignore = currentJob.jobConfig.ToObject(t); //jobConfig is a JObject

The cast works fine, but by doing the cast, somehow the exception mentioned above and shown below is triggered screenshot of my visual studio exception

Edit #2: Repairing, updating and reinstalling visual studios has not changed the issue, the app still seems to work fine 10-40% of the time, and crash the rest of the time

The Lemon
  • 1,211
  • 15
  • 26
  • 1
    Sounds like a corruption. First id try running *VS* in *safe mode* to see if its an extension. then if that fails it try the following in this order. Upgrade / repair / reinstall – TheGeneral Aug 19 '20 at 04:23
  • @MichaelRandall unfortunately all four things did not fix my issue, are there any other steps you can recommend? – The Lemon Aug 19 '20 at 06:52
  • 1
    Try it on another machine. It's either your app doing something interesting that causes corruption, or some third-party tool/hook on your machine that's doing something interesting, and using another machine (preferably a clean VM that has nothing but VS) would allow you to distinguish between this. You can also use a tool like Process Explorer with DLL view in the lower pane to pre-emptively scan for things injecting themselves that might be causing trouble. – Jeroen Mostert Aug 19 '20 at 06:55
  • it seems to crash fairly reliably on any machine (yay), I'm going to try turning off large sections of my code until I zero in on the set of conditions causing this, thanks for the tips – The Lemon Aug 19 '20 at 07:38
  • 1
    I have more or less the same problem, and I don't think your application crashes. You just have a unhandled exception. When you hit F5, great chance your application continues. But in my case it is very annoying that I cannot ignore this exception in the Exception Settings even though the Break on SEHException checkbox is unchecked! I have to press F5 about 20 times before I can continue my application. – ffonz May 31 '22 at 06:54

1 Answers1

0

I found a solution to my problem, by adding 'return;' statements everywhere and removing them one by one (removing large sections of my code). I eventually found the line that was causing problems. It turns out, if you serialise and then deserialise a CancellationToken, visual studio's debug session becomes prone to randomly crashing when, presumably, it tries to do a certain operation on the object you have deserialised to. The solution was just to make the cancellation token either private, or add [JsonIgnore]

Code to reproduce the problem: (run the void method, and then try closing the window)

    private void BreakVS()
    {
        ClassWithCancellationToken someClass = new ClassWithCancellationToken();
        someClass.ts = new CancellationToken();
        var json = JsonConvert.SerializeObject(someClass);
        JObject jObject = JObject.Parse(json);
        var test = jObject.ToObject(typeof(ClassWithCancellationToken));
    }

    public class ClassWithCancellationToken
    {
        public CancellationToken ts; //change to private to 
    }

The answer to my original question then, is to start with Jeroan's solution to try debugging on a different device, if the code still breaks on said device then try what worked for me (hack and slash your code until you find what's breaking it) - or a more sophisticated approach to debugging your code more thoroughly. If your code works on another device, then try Micheal's approach to update/repair/reinstall your Visual studios install

Of course, the one thing I still don't know how to do, is to locate the faulty code without spending an hour removing sections of my code until I find the cause.

The Lemon
  • 1,211
  • 15
  • 26
  • 1
    That's not it. You merely rearranged the furniture, this memory corruption problem will smash your knee-cap in another place at another time. If you can't get a code review then calling Microsoft Support for assistance tends to be worth the money. – Hans Passant Aug 19 '20 at 12:03
  • But that section of code causes the same issue in a completely clean/new application, the code crashes for me on multiple machines, installs of VS, and applications - are you sure that isn't my problem? – The Lemon Aug 21 '20 at 01:19