Crash when exception is thrown from .NET 9.0.4

18 visualizaciones (últimos 30 días)
Brian
Brian el 14 de Abr. de 2025
Comentada: Brian el 15 de Mayo de 2025
MATLAB crashes when an exception is thrown from .NET code and .NET 9 is installed. Here is a reproducer:
public class MatlabTest
{
public void ThrowException()
{
throw new InvalidOperationException("This is a test exception");
}
}
And in MATLAB:
dotnetenv("core");
NET.addAssembly(fullfile(pwd, '.\MatlabTest.dll'));
test = MatlabTest.MatlabTest();
test.ThrowException();
The crash seems similar to Why does Matlab crash when I try to access .NET 6.0 (core)? Indeed the error occurs only when .NET 9 is installed on the machine. It seems like that issue was resolved in .NET 9.0.4 so we upgraded our .NET 9 runtime to 9.0.4, but the issue still exists.
This is what it looks like without .NET 9 installed on the machine (MATLAB always uses the latest .NET runtime):
Following is some of the crash information reported by MATLAB:
Assertion in void __clrcall dotnetcli::HandleBackgroundException(class System::Object ^,class System::UnhandledExceptionEventArgs ^) at B:\matlab\external\interfaces\dotnet\dotnetcli\src\NetPackage.cpp line 81:
Managed .NET code threw an exception. Consider using System.Threading.Tasks.Task APIs. These ensure managed exceptions return to the main thread where they can be handled in MATLAB code.
The following .NET exception caused MATLAB to crash:
System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.
at dotnetcli.throwMException(SByte*, Char*, mxArray_tag*)
at dotnetcli.rethrowTargetInvocationExceptionID(TargetInvocationException e, BaseMsgID* msgID)
a

Respuesta aceptada

Helen Zhu
Helen Zhu el 14 de Mayo de 2025
Movida: Walter Roberson el 15 de Mayo de 2025
The crash can occur with R2023b, R2024a, R2024b. The issue is not present with .NET 8.
By default, MATLAB always uses the latest .NET runtime.
If you have .NET 9.0.4 and 9.0.5 or the .NET 10 release candidate installed on your machine, you can change the .NET runtime that MATLAB uses:
  1. R2023b: You must edit the matlabroot\bin\win64\dotnetcli_netcore.runtimeconfig.json file to specify the .NET version in R2023b
  2. If there is a unique directory containing the .NET version you want, you can set DOTNET_ROOT env var before loading .NET, and MATLAB will search for the highest version in that directory instead.
  3. Starting in R2025a you can use the Version= and Frameworks= name-value-pairs to select a specific .NET version. System Requirements for Using MATLAB Interface to .NET
  2 comentarios
Brian
Brian el 15 de Mayo de 2025
Can you please provide a bit more information on workaround number 1 above? Here are the contents of my dotnetcli_netcore.runtimeconfig.json file:
{
"runtimeOptions": {
"rollForward": "LatestMajor",
"framework": {
"name": "Microsoft.WindowsDesktop.App",
"version": "6.0.0"
}
}
}
What should I put in this file to use e.g., dotnet 8.0.16?
Brian
Brian el 15 de Mayo de 2025
Nevermind. It was pretty easy to guess.
{
"runtimeOptions": {
"framework": {
"name": "Microsoft.WindowsDesktop.App",
"version": "8.0.16"
}
}
}
>> dotnetenv("core");
>> System.Environment.Version
ans =
Version with properties:
Major: 8
Minor: 0
Build: 16
Revision: -1
MajorRevision: -1
MinorRevision: -1

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Productos


Versión

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by