Skip to content

How to get native exception on .NET core or .NET 5 #54448

@diehell02

Description

@diehell02

I need to get the call stack when a crash occurs.

Our project is using C++/CLI to combine C# and C++ code.

The exception from C# and C++/CLI, we can use try-catch to get the exception.

But the exception from native C++, especially the exception that is considered unrecoverable, or occurs in another thread, like AccessViolationException, StackOverflowException.

int* p = nullptr;
*p = 123;

And I also see when the native exception occurs, the console will print this message and then the app crash:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Repeat 2 times:
--------------------------------
   at <Module>.NativeCppDemo.MyNativeCppTestClass.memberFunctionAddFunCrash(NativeCppDemo.MyNativeCppTestClass*, Int32, Int32)
--------------------------------
   at ClrCrashTestNetCore.ClrTest.memberFunAddCrash(Int32, Int32, Boolean)
   at CsCrashDemoNetCore.Program.Run()
   at CsCrashDemoNetCore.Program.Main(System.String[])

This is the message that I want but I don't know how to get it in the code.
And I found this message when the exception occurs on another thread, it won't print on the console.

Do we have any method to get the exception information from native C++? Like has some interface to get the exception? Or using a profiler can get the call stack?

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-ExceptionHandling-coreclrquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions