Exception handling inconsistency when using Hostfxr to call a callback method in a try catch on ubuntu

105 Views Asked by At

Im playing around with the hostfxr sample and got it working a bit but im running into an issue when using exceptions n c++

The setup I have now is that the NativeHost.cpp has a method that throws an error named ErrorHandler. This method is passed in a try catch to c# and is called there. Now Im expecting that the catch for std::invalid_argument will be entered when the error is thrown in the ErrorHandler method. Now for windows this works, but on my Ubuntu VM it just aborts and throws the invalid_argument. Where am I going wrong on this?

Simplified Sample(Full repro https://github.com/gertkommer/HostFxr-exception-issue)

//NativeHost.cpp
static void ErrorHandler(){
    throw std::invalid_argument("TEST");
}

int main(){
    //TAKEN OUT - Load netcore stuff and functionptr to c# function InvokeCallBack
    try{
        InvokeCallBack(ErrorHandler)
    }catch(std::invalid_argument e){
        std::cout << "This is a test to see if i get in the catch";
    }
}

//Lib.cs
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
public delegate void ErrorHandler();

public delegate void InvokeCallBackDelegate([MarshalAs(UnmanagedType.FunctionPtr)] ErrorHandler function);
public static void InvokeCallBack([MarshalAs(UnmanagedType.FunctionPtr)] ErrorHandler function)
{
   function();//Call ErrorHandler here
}

Expected output
On both windows and ubuntu This is a test to see if i get in the catch

Actual output
Windows
This is a test to see if i get in the catch
Ubuntu 20.04
terminate called after throwing an instance of 'std::invalid_argument'
what(): TEST

0

There are 0 best solutions below