MATLAB: Does an EXE generated from MATLAB Coder throw a Stack Overflow exception during execution

coderdllexematlab coderoverflowstackwindows

I'm looking to generate a standalone EXE from their MATLAB code. The code generation process within MATLAB is successful, but when I run the EXE,, I find that it does not run to completion. Instead, I receive the following message:
Unhandled exception at 0x00007FF69CC9C298 in run_iterations_test.exe: 0x00000FD: Stack overflow (parameters: 0x0000000000000001, 0x00000A338C63000).
However, when I generate a DLL and call that within MATLAB using "loadlibrary", I do not see any error. What is going wrong?

Best Answer

The stack overflow seems to happens at the following point in your EXE
catapult_vs_F0PS_sim_4d193148_1_ds_duf(const NeDynamicSystemTag * sys, const NeDynamicSystemInputTag * t1223, NeDsMethodOutputTag * t1224) at Line 14
It seems like you have local variables within the above function that require more than 1800 Kb. In Windows, the default stack size is 1 Mb, and so it makes sense that the stack overflows. Increasing this stack limit to 10 Mb makes the crash go away. When using "loadlibrary", the stack limit is controlled by the caller process, i.e. MATLAB, which sets the limit to more than 1 Mb.
On your end, if you increase the Windows stack size, the application will run successfully. Below are two ways to change the stack size: