Clear Filters
Clear Filters

Mex Lv-2 S-function generation with VS2019, Debug build causes strange Matlab/Simulink Crash after 'clear mex'

4 views (last 30 days)
I have done a lot to try to troubleshoot this myself but need help. I have replicated the problem on a second machine so I know it's not a local issue. Here are the details: Using matlab 2021a
We used to generate our mex files using the builder block GUI, we have the box checked to compile a debuggable mex function. Using the Visual Studios compiler options, this generated a .mexw64.pdb, which we could use with the VS local debugger to set break points and monitor locales. Everything worked fine. As the project grew we switched to using the mex cmake tools and visual studios to compile the project. That is when the issue started. Our workflow starts by generating the wrapper source code using the GUI and then letting cmake do the rest of the build system construction.
If I compile the mex using the cmake build system in 'Release' mode, it works perfectly fine, but cant be debugged. If I were to compile with debug mode in visual studios (so it creates a PDB), it will run fine after a fresh instance of matlab is opened but if I use the command clear all, or clear mex, to release the mex so that I can recompile a change to its source code. Then when I hit play on the simulink model, it crashes at initialization. Just to summarize, when I only use the debug build, it runs fine until the mex is cleared, instead of just reloading it, it crashes.
We notice if you first load in the Release build, clear mex, and then switch the build from release to debug, everything works as expected, we can even use the debugger in VS2019 to add a breakpoint. But if we restart matlab, and just use the debug build, we have problems.
There is no error message that I can find which correlated to the issue in the matlab logs but which using the VS debugger attached to matlab.exe I see this error just before a crash:
Unhandled exception at 0x00007FFB8FDBD4C0 in MATLAB.exe: 0xC0000005: Access violation executing location 0x00007FFB8FDBD4C0
Please note:
  • we have tried a fresh install
  • we are sure there is only one instance of the mex file on the path when running
  • we have tried toggling the debuggable setting in the GUI to generate the wrappers
  • we have tried updating the model and recompiling the model before running again, but no luck
I think in some way matlab just doesn't release the .pdb or maybe it locks the file, which is odd because it has no issue with the .mexw64.pdb created by the built-in gui when recompiling the mex. Any insight is appreciated

Answers (1)

Karanjot
Karanjot on 15 Dec 2023
Hi Noah,
I understand that you have encountered an access violation crash during a Simulink simulation.
The crash reporter in Simulink simulation is indicating that there was an access violation error during the simulation. This error occurs when the program attempts to access memory that it is not allowed to access, resulting in a crash. Crashes may happen due to an error internal to MATLAB.
I request you to share the entire crash report to detect the issue. The crash report contains one or more crash logs containing information on the stack trace and the MATLAB configuration. You can see the entire contents of the crash report by clicking the 'Show Report' button in the dialog box.
To learn more about this, please refer to the webpage below:

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!