C# program quitting/crashing suddenly, how to break it? - c#

So my C# program is quitting/disapearing/closing/crashing suddenly even in debug mode.
How can I make it break/pause/stop so as to find out where caused it?

Set a breakpoint (by clicking the left of the line numbers--it's a red circle) that you know is before the point where it crashes and then step through it (using F8). The last statement that you're on is the one that it's crashing at.

If you're in Visual Studio you can enable "Stop when an exception is thrown" - go Debug->Exceptions. This is often quite useful for detecting the exact source of the problem.

You may be able to learn more by using an unhandled exception handler. However, if you're in debug mode you should be able to see it by default. Check your Debug: Exceptions settings to make sure you haven't accidentally stopped reporting them in debug mode.

as Eric suggested try going through step through it... also always place try/catch in your end caller apps in order to manage exceptions.

Also, it may be worth checking your code for occurrences of Environment.FailFast, just in case someone tried to be (too) smart.

You can hit F10 to start debugging your project. This will put a breakpoint right at the beginning of your Main method, then you can go from there stepping through the code with F11 to see at which points it crashes.

Related

Visual Studio 2012/C# breaks code execution, no breakpoint set

I'm having a problem in Visual Studio 2012/C# which is driving me crazy. I have a particular line of code in a C# file with NO breakpoint set. Every time this line should be executed the debugger interrupts the execution of my code like a breakpoint was set. The only difference i noticed is the arrow on the left hand side, indicating the current step. Usually this arrow is yellow (when a breakpoint is set). In my case it is grey.
I have been searching for a solution but did not find anything useful to this. I tried deleting all breakpoints, build project, rebuild project, clean project and it still appears.
Does anyone have the same problems and found a solution this?
normal arrow:
arrow in my case:
This line of code is currently executing, it calls something else, e.g. native code or .NET internal code and an exception happens there. You can see both arrows in the call stack window:
Perhaps you need to turn on "Show external code".
If you can't see the exception dialog, usually you can show it like this:

How to find source for win32 exception "error creating window handle"

I'm looking for a little help regarding the win32 exception "error creating window handle".
From time to time our program (WinForms - C#) throws this exception and sometimes the windows even freeze, so that the users have to kick the process to be able to work again.
From the many other threads regarding this problem, I know what I should be looking for, but not extactly where as our program is quite big. So I was hoping that there might be a way to restrict the codelines I have to check... Are the any tools, which can help with this exception?
Short update: I solved the problem.
ProcDump didn't help me much, because I got pretty the same information from our logfiles. However, I was able to reproduce the error in our development environment. Thanks to debugger and displaying the user-objects count in the taskmanager I found the source of the memoryleak - an undisposed texbox which was created dynamically.
Thanks again for the tips!
If this issue happens inside the debugger, you can set the debugger (I will assume Visual Studio as you're using C#?) to break on throwing an exception. In your case you would want to set an exception breakpoint on (I think) System.ComponentModel.Win32Exception.
Again assuming Visual Studio as your IDE, on the "Debug" menu is an "Exceptions..." item. This allows you to tell the debugger to break when specific exceptions are a) thrown, or b) unhandled.
Under Common Language Runtime Exceptions, expand System.ComponentModel, and enable the checkbox in the Thrown column for System.ComponentModel.Win32Exception.
Then just carry on as normal. If during debugging the exception occurs, it should break into your program and allow you to see where it's happening.
Edit: If you can't reproduce the problem on your development machines, see if you're able to set up the target machine to produce a dump when a crash occurs. One way of doing this is to run ProcDump. Run it with the -e parameter to create a dump in the event of an exception. Then you can analyse this back at the ranch.

Visual Studio 2010 Pro, not ending debug mode on application close

When I run my code within VS, when I enter certain methods, upon ending the application (closing the form) been debugged, VS does not end the debug session automatically forcing me to end it manually.
This doesn't always happen, the results are consistent with certain windows / classes loaded when the application is run within VS.
I want to know why this may be, I'm assuming its an indication of a file not been closed in my managed code or 'something' like that.
Why might this occur and how can I trace and fix it?
This would happen if your code keeps running after closing the window.
Pause the debugger after closing the window to find out what it's doing, then modify the code so that it stops running when you close the window.
On Debug menu, click "Detach All".
Hope that helps!
EDIT
Follow, SLak's advice, as that will go after the problem. My solution may only fix the symptom. I will leave the answer here just in case it is helpful to you though.
Good luck!

Visual Studio is missing/moving my breakpoints

The problem is that when I place a breakpoint and debug/run, the breakpoint moves by itself.
Before/whilst coding:
After clicking run/breakpoint hit:
Breakpoints typically work ok for me, but it seems that they sometimes randomly play up. I first had this ages ago with a VB project, and in the end, I bypassed the problem by removing the breakpoint and adding it somewhere else where it was still useful.
Whilst I could probably do the same again, and this is only the second time it has happened (that I remember), I don't really want to have to and would like to know what is actually wrong.
I have read through many similar questions here, but I cannot see an exact match and the answers do not help. I have tried - building, rebuilding, closing/reopening and cleaning.
I only provided a picture of the bit of code where it occurs, if you need anything else, please let me know.
It's because the debugger isn't able to break at that point. For example, the debugger can't break on auto-implemented properties, or at the header of a method; instead, it breaks at the first line of the method.
Also check to see if you had set the breakpoints in one mode (Debug) but are now in a different mode (like Release). This caused me some momentary angst.
I got this problem after Windows had inexplicably added 6 months to the current system date (and 1 hour to the time). I didn't correct this right away, so builds made before the correction looked more recent to Visual. This lead to running the wrong (older) build when debugging.
Since I couldn't find a "Clean Solution/Project" option, I had to manually delete all .pdb files in the solution. The problem was instantly fixed. I just hope there aren't any more files I might need to delete (I'm new to Visual and don't know much about how it works behind the scenes).
You can get this if the breakpoint is AFTER a return statement. Visual Studio will bounce the breakpoint to the closing brace (or "End Function" for VB.NET) because return is jumping out of the function.

Break the debugger on assertion failed

Is there a way to break the debugger when assertion is false and running the application using Visual Studio debugger. Earlier when I was debugging Windows application I would get an exception and the debugger would break, but now on Smart Device an assertion failed window is showed with stack trace, but I would also like to see variable values etc.
Stupid me, the solution was simple. When the window pops out, press pause in debugger :)
Not sure about VS 2008, but in at least 2010 and later, you can go to Debug/Exceptions (Ctrl-Alt-E). Click the "Add" button, choose "Common Language Runtime Exceptions", and type:
Microsoft.VisualStudio.TestTools.UnitTesting.AssertFailedException
and hit "OK". It will now appear on the list, make sure you check the checkbox in the "Thrown" column. You will now get a break on any assert failure.
Updated:
screenshot from VS 2017
In addition to Vinay's solution, you can start the debugger for a specific process by calling
Debugger.Break
In your case you could do it every time the listener receives a failure message.
It seems that you can attach the Debugger when assertion fails to see other details - see this article: http://blogs.msdn.com/b/davidklinems/archive/2005/08/29/457847.aspx. Its quite dated but perhaps still applicable.

Categories