Stack overflow using the generated C++ code

25 visualizaciones (últimos 30 días)
Lukai Cai
Lukai Cai el 23 de Feb. de 2018
Respondida: Lukai Cai el 27 de Feb. de 2018
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 comentario
Denis Gurchenkov
Denis Gurchenkov el 23 de Feb. de 2018
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.

Iniciar sesión para comentar.

Respuesta aceptada

Lukai Cai
Lukai Cai el 27 de Feb. de 2018
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.

Más respuestas (2)

Lukai Cai
Lukai Cai el 23 de Feb. de 2018
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!

Lukai Cai
Lukai Cai el 23 de Feb. de 2018
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 comentarios
Denis Gurchenkov
Denis Gurchenkov el 23 de Feb. de 2018
Or just contact MathWorks technical support if you are not comfortable posting source code online.
Lukai Cai
Lukai Cai el 23 de Feb. de 2018
Editada: Lukai Cai el 23 de Feb. de 2018
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.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB Coder en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by