C#: How to log stack trace of AccessViolationException before application crash? - c#

I have some unsafe methods may throw AccessViolationException, it will stop at the line of code with debugger attached. but if run the application without debugger, the application just crashes, cannot log the stack trance to file.
Is there any way to find out which line causes the exception?
I'm using .NET 6

Related

LibreOffice Stopped working , while converting XLS to XHTML files using C# application

In this, my queries are,
How to handle LibreOffice Exceptions in C#.
If could not handle , how to pro-grammatically close this exception or to suppress this exception popup, while excuting console
OR any better solution, that my conversion process , should not stop because of unhandle exception caused by Libre Office.
From the image, it looks like the exception got thrown from storeToUrl() in the code at https://github.com/caolanm/unoconv/blob/master/unoconv.
unoconv is a command line tool, not a library, so it does not seem possible to catch the error with a try/catch block. Perhaps you could capture the process's error stream and parse it to see if an exception occurred.
Instead of using unoconv, it may be better to call storeToUrl() directly in C# using the UNO API, with a try/catch for com.sun.star.io.IOException.
When doing many automated conversions with LibreOffice, it is typically necessary to kill the process whenever it gets stuck: https://github.com/dagwieers/unoconv/issues/352.
Firstly, thank you all for your kind help.. :). Finally i resolved.
While bulk file conversion , my c# process, was getting interrupted , because of some exception occurred in LibreOffice process(soffice). so i started killing my c# process and its child processes and also killed soffice. So for next file, fresh soffice is getting created , as i executed unoconv command.
but still, i get the exception popup, but the c# process , keeps continue..
this code helped me

Check InnerException in System.TypeInitializationException thrown by an external code frame

Using Visual Studio 2012 Express, how can I check the InnerException when a System.TypeInitializationException is raised from an external code frame. The debugger breaks due to the exception, however not at any code line. It gives the following message:
No Source Available
The call stack contains only external code.
Thi thread is stopped with only external code frames on the call stack. External code frames are typically from framework code but can also include other optimized modules which are loaded in the target process.
And then gives the call stack, See below:
I would set a try-catch block, but I don't know where in the code this happens. How can I inspect the InnerException at this point? Is there another way to go about finding what's up?
Repro code for such an exception where the stack trace looks exactly like yours:
class Program {
static Program() {
throw new Exception("kaboom");
}
static void Main(string[] args) {
}
}
You should use the Exception Assistant that pops up when the exception is thrown. Click on "View Detail":

Debug an issue in an underlying C++ DLL

I have a main application (in C#), which parses an image database, and then pipes those images into a C++ DLL to have them analysed. Now for some reason every 200 images or so, it throws an error. It doesn't always throw the error at the same image, but just at a random image (sometimes an image passes though the analysis, the other time it throws an error).
Can I somehow make my DLL throw an exception to the C# GUI with information on where in the C++ code the error originates from? The code itself should run fine, and I cannot find where the error is coming from, so I need some help from the DLL to identify at least where it happens.
Any help would be appreciated, I found nothing on that so far.
Just use the debugger to diagnose this. Enable the unmanaged debugger with Project + Properties, Debug tab, tick the "Enable unmanaged code debugging" option.
"it throws an error" is too vague to give specific advice. But you'll want to check the Thrown checkbox in the Debug + Exceptions dialog. Tick "C++ exceptions" and/or "Win32 exception", depending on the type of exception that's being thrown. If you don't know then tick all of them.
If you are not able to get the error when debugging it, in your C++ dll write to output the function that you are in.
For instance, if you have func1, func2, func3 in the dll then write out their names every time you enter and exit each function. When you run the program you will be able to narrow it to the function that is causing the exception, then you can add similar outputs after each line in the function to find the code that is throwing the exception.

Forcing an app to close when getting error 0xc0000005

I have a C# WPF app that display an error dialog with the error code 0xc0000005. I researched this error code and found that it is a access violation error and could be caused by several things including native code errors (p/invoke and 3rd party dlls). Restarting the app seems to clear the error but I want to be able to force the app to close when that error occurs. Since it is not a managed code exception it is not caught in the try catch blocks, is there any way to force the app to close when this error occurs?
You can catch native exceptions in different way. Either using Win32Exception or SEHException exception class or using catch without any exception type specified as
try
{}
catch
{}
Refer this for details: Can you catch a native exception in C# code?
Use Environment.Exit(0); to terminate your application.

Why do my exception stack traces always point to the last method line?

I have a problem with my Visual Studio installation. When I got an exception I always have incorrect line numbers in it's stack trace. There are always point to last line of each method in my codebase. At the same time it's OK when I'm tracing programs with debugger. What's happed with PDBs?
No, I'm not re-throwing exception at each method.
In each line of stack trace I have last row of corresponding method, while exception had thrown by statement in the middle.
Sounds like you're running your app in Release mode. Release mode has difficulties with line numbers for exceptions and whatnot.
Compile your app in Debug mode (no need to attach the debugger) and see if it sorts itself out.

Categories

Resources