Stack overflow using the generated C++ code
显示 更早的评论
I generated the code using Matlab Coder, and integrated into MSVC of another project. During run time, it gives stack overflow error. I tried to use memory optimization feature (Enable variable-sizing, and reduce stake usage max to 100,000). The simulation goes a little further but still give stack overflow in another function call. Just need to know how to address it.
Really appreciate it!
1 个评论
Denis Gurchenkov
2018-2-23
Hi Lukai,
- If you generate a MEX file and run it from MATLAB directly (without integrating your code with another project), does it work on same input data? Or does it give you some error?
- If you run your project with MSVC debugger attached, does it give you a stack trace? -- Check the stack trace for signs of infinite recursion (same functions called again and again) -- Check the bodies of functions involved for presence of local variables of huge sizes (e.g. real_T buf[100000]) -- If you have Embedded Coder, use the "static code metrics" link in the compilation report, there you can see stack sizes for all functions in the generated code.
hth,
Denis.
采纳的回答
更多回答(2 个)
Lukai Cai
2018-2-23
0 个投票
5 个评论
James Tursa
2018-2-23
So abc is an array of some type of object, and sizeof(each object) = 153600? Is that where the 4608000 = 153600*30 comes from?
I guess the general response would be to increase the stack size on your project, or to change the memory allocation scheme to get the memory for abc off of the heap (using malloc or new etc) instead of the stack.
Denis Gurchenkov
2018-2-23
According to this https://msdn.microsoft.com/en-us/library/windows/desktop/ms686774(v=vs.85).aspx the default stack size for a thread on Windows is 1MB. sizeof of abc is 46MB, so it should cause a stack overflow if that function is called.
I think you need to find where that variable is coming from -- what is the MATLAB's data structure or variable that is the source for this C variable?
Things you can try:
- Disable inlining (codegen ... -O disable:inline ...) -- this would make it easier for you to see which function this variable originated in.
- If you can find the original MATALB variable that corresponds to "abc", look find it in the compilation report and see why it is so large. Presumably there is some matrix inside that variable that has a huge size.
Denis Gurchenkov
2018-2-23
Can you post your MATLAB code (or an extract from it) and corresponding C code that shows the large variable?
Denis Gurchenkov
2018-2-23
Or just contact MathWorks technical support if you are not comfortable posting source code online.
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Coder 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!