Why do I get "Access violation reading location 0xFFFFFFFFFFFFFFFF" error while running .NET application which uses .DLL generated by MATLAB Coder R2024a?

I am generating a .DLL from my MATLAB Code using MATLAB Coder R2024a. When I call the generated entry-point function in my .NET application and run it, my application crashes with the following error message: 
Access violation reading location 0xFFFFFFFFFFFFFFFF
It seems that some pointers defined in my .NET structures are likely being freed or becoming invalid before the C code finishes using them.
However, I'm not sure how to properly avoid this. My .NET structures are designed after the corresponding C structures found in the generated <dll_name>_types.h header file.
What is the root cause of this error, and how do I fix it? 

 Accepted Answer

The best practice is to use the “emxArray” utility functions to handle pointers, memory, and general handling of arrays instead of managing the fields manually in .NET. For more information on that, please refer to MATLAB documentation on how to use C arrays in the generated function interface
These functions already have implemented a lot of the logic that you might be using in your .NET application. While implementing your .NET application, you might have missed some of the steps that are performed under the hood by these utility functions, which is why that is the recommended way.  
The generated code would also contain an example of a main function in the file 'main.c', which would show how these APIs are called. Please refer to that for more information. 

More Answers (0)

Products

Release

R2024a

Community Treasure Hunt

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

Start Hunting!