How do I give C++ child threads more stack memory in MATLAB on Windows?

I am using the "NET.addAssembly" function to load a .NET DLL, which is then used to call a C++ DLL on Windows. This C++ DLL spawns child threads that consume a lot of stack memory. In R2019b, this was no problem, as it appears the C++ thread is given about 64 MB. However, when I updated to R2025b, I started getting stack overflow errors. It seems that the threads are only given 1 MB. How can I give the child thread more memory?

 Accepted Answer

The C++ thread inherits the stack size of the MATLAB process. Starting in R2020a, the MATLAB stack reserve size was reduced from about 64 MB to 1 MB, which is the default for Windows. Consequently, there was a corresponding reduction of stack reserve size in C++ threads spawned from MATLAB. To work around the issue, you can do either of the following: 
  1. If you have access to the source code creating the thread, use the Windows API "CreateThread" to set an explicit stack size when creating the thread, and set the STACK_SIZE_PARAM_IS_A_RESERVATION flag.
  2. Modify the binary's stack reserve size using the following command in a Visual Studio Developer Command Prompt:
    > editbin /STACK:67108864 <matlabroot>\bin\win64\MATLAB.exe
The better long-term approach is to modify the code to support the desired stack size, but if that isn't feasible, modifying the binary is a valid workaround.

More Answers (0)

Categories

Products

Release

R2025b

Community Treasure Hunt

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

Start Hunting!