I would like to terminate application when error occurs, something similar to throwing an exception that cannot be caught.
When C++ exception is thrown and not catched, Visual Studio 2017 automatically opens the file where exception is thrown and points to the exact line of exception in a text editor, which is exactly what I want, except that I would also like to prevent exception from being caught.
I tried using abort(0) and std::terminate(), but then a message box is shown, instead of pointing to the line in text editor where application is terminated.
Simillary, exit(0) only displays message to debugger output which says that application has exited.
Is there a way to terminate application, but still make Visual Studio point to the line in text editor where application is terminated?
Related
Am trying to use the project found here, I have made some modifications and the code works okey, but sometimes it throws the below error.
How can I suppress or handle that error message in code, as it stops logic flow from continuing untill the button is pressed manually
after debgugging abit, I can find the error is thrown in a delete line, it tries to delete a pointer and it prints std::bad_alloc then hangs
I have a C++ application that suddenly stops working when I do a certain action (clicking at a button in gtk). I tried debugging it, creating Signal Handlers for SIGTERM, SIGABORT, SIGILL, etc to write a backtrace with the gcc functions, tried to attach a debugger, etc. Nothing. I just do not get any output from anything, the program just vanishes from the memory.
Are there any techniques I have not tried yet? I dont know how to debug this problem.
I forgot to mention: This happens on a Linux system (tried debian and ubuntu). Both with X11 (not wayland)
It could be:
A signal. By default, gdb stops on error signals, so no custom handler is needed.
exit() function and any other function from exit family (like _exit, _Exit , etc.). Use b exit to set a breakpoint.
Since it's c++, and exception could be thrown. Use catch throw to stop when an exception is thrown.
The last thread exit. b pthread_exit.
Thread cancellation. b pthread_cancel.
main function normally reached its end. Use disassembly to set a breakpoint on address.
If all of this doesn't help, attach to your application before the button is pressed, pause it with Ctrl+c in gdb. Then, press the button (while the window is unresponsive). A gtk application should normally dispatch the event from X11 queue even if it was paused. Do step by step assembly debugging with ni and si.
I'm writing a few tests for a managed/unmanaged Winform application. Some of the bugs occur in the unmanaged part, and result in the process terminating due to an unhandled System.Runtime.InteropServices.SEHException exception .
When this exception occurs, Windows pops up a message box explaining the error.
Unfortunately, neither MS Test nor White recognize this. The test itself finishes successfully without any sign of error, even though I can see the message box pop up right before the test goes on and closes the application.
How can I detect this kind of exception?
The message box is the result of the default unhandled exception handling of a Winforms application.
I ended up looking for it after the test has finished. If the message box is there, I fail the test.
In the Microsoft Windows API, you can use SetUnhandledExceptionFilter, to set a handler for unhandled exceptions. The big catch, mentioned on that page, is:
If an exception occurs in a process that is not being debugged, and
the exception makes it to the unhandled exception filter, that filter
will call the exception filter function specified by the
lpTopLevelExceptionFilter parameter.
(emphasis added)
Which basically means, if the process is getting debugged, the debugger gets the exception, and my filter is skipped!
I can test & debug my ExceptionFilter the old-fashioned way, with printfs and trial-n-error.
But am I missing something? Is there a good way to interactively debug an ExceptionFilter if it is disabled when in a debugger?
Checkout the Resolution section of KB173652 which talks about placing all the code in main/WinMain in a _try/_except block like the following.
void main (int argc, char **argv)
{
__try
{
// all of code normally inside of main or WinMain here...
}
__except (MyUnFilter (GetExceptionInformation()))
{
OutputDebugString ("executed filter function\n");
}
}
Another article, Debugging custom filters for unhandled exceptions, describes a couple more techniques in addition to the one above. I personally use the one where you display a message box inside your exception filter and then attach the debugger. I use IsDebuggerPresent to determine whether to display the message box or not.
I know this post has been around for a while, but, I just happened upon it searching for something else. I’m happy to say that what user ‘abelenky’ asks is possible if the filter exists in a separate dll. You can debug an unhandled exception filter using a debugger. I’ve done it, and, here’s how:
The exception filter must exist in a separate dll. You’ll see why later.
You’ll need to add some code to the filter that displays a message box. I use the following code:
#ifdef _DEBUG
AfxMessageBox (_T("At this time, you must attach the debugger to this process in order to debug the filter code."));
#endif
The #ifdef is important because you don’t want the code executing in a Release build. I placed the above code at the very top of my filter.
To debug the filter:
Build a Release version of your application in Visual Studio
(instance #1).
Build a Debug version of your filter in a second instance of VS (#2).
Copy the Debug version of the filter to the Release folder of your
application.
Start your Release application from the Debug menu “without
debugging”.
Cause a crash in your application.
When the debug message box (above) appears, change to the second instance (#2) of Visual Studio.
In the #2 instance, open the filter project in Debug (if it isn't open) and attach the
debugger to your Application instance.
Set a breakpoint in your filter code after the message box displays.
Close the message box and your breakpoint should be hit.
Continue to debug your code.
I have added an "Open file" dialog to my dialog-based MFC application. Now, exactly one minute(!) after an open file dialog is closed by pushing either Open or Cancel button my application crashes. While it crashes, the following things are happening in the output:
1) a bunch of Windows threads are exiting;
2) a bunch of COM exceptions (of 0x80010108 "the object invoked has disconnected from its clients" and 0x800401FD "Object is not connected to server" variety) are being thrown;
3) finally, an unhandled exception occurs: 0xC0000005: Access violation reading location 0xfeeefeee, with call stack pointing to ole32.dll.
To say that I am bewildered is quite an understatement. The code for invoking the dialog is as follows:
CFileDialog fileDlg( TRUE, _T(".txt"), NULL, OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST,
_T("Text file (*.txt)|*.txt||"), this);
INT_PTR res = fileDlg.DoModal();
What could cause such a thing?
How do I even debug it?
I had this exact issue in Windows 7 x64, and by enabling breakpoints on all Win32 exceptions not already chosen (in the VS2015 exceptions tab), I was able to narrow it down to a known issue with fundisc.dll that was resolved with an optional Hotfix from Microsoft: https://support.microsoft.com/en-us/kb/2494427
It also resolved issues on my PC of File Explorer windows crashing at seemingly random times. All were caused by some deadlock in the networking COM objects that is fixed by that hotfix.