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 个评论

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.

请先登录,再进行评论。

 采纳的回答

Finally I got the solution. The issue is not static variable. The issue is that the function variable size is too big.
Here is the solution: when generate the code. in the memory configuration, by default Stake usage max is 200,000. But for MSVC, the default is 100,000. So I reduce it to 50,000 (consider other functions use another 50,000). Also I enable "Variable-sizing". And change the dynamic memory allocation threshold to 4000 (by default,it is 65,536). I use 4000 because it has many variables with double[600] type (size of 4800), thus I enforce the tool to use static variable instead of regular variable. Once it is done. No stack overflow issue anymore.
Thanks for the help from Denis.

更多回答(2 个)

yes. the data structure contains a big array inside it. Regarding changing the memory allocation scheme, how to do it in Matlab? Any guideline for this? The code is automatically generated.
BTW, I am beginner of the matlab. Thanks a lot!
If I run it with simple main.cpp. It works. If I integrated into a working but complicated project I want to, it doesn't work. It is not stack regression It has large variable.For example, it has variable named abc[30] (declared as static) in function named F_A, with sizeof(4608000). And F_A further calls F_B using abc[30] as parameter. )
Are they the problems? Any guideline of how to avoid these issues.

5 个评论

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.
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.
Can you post your MATLAB code (or an extract from it) and corresponding C code that shows the large variable?
Or just contact MathWorks technical support if you are not comfortable posting source code online.
Thanks Denis. Here is the high level details:
There is a matlab function: function [tx_drv, ..] = tx_gen(...)
In the function: tx_drv is an array of one_tx with size 30 for i = 1 : tx_num tx_drv{i} = one_tx; end
one_tx is 30x640 array.
When generated in C++ typedef struct { double f1[19200]; } cell_wrap_0;
tx_gen(cell_wrap_0 tx_drv[30], double tx_polarity[900]);
Not sure if it is the issue. If need further discussion, I will contact MathWorks technical support.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 MATLAB Coder 的更多信息

产品

标签

Community Treasure Hunt

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

Start Hunting!

Translated by