tinyexpr
tinyexpr copied to clipboard
Possible memory leak in te_compile
I'm getting the following crash report on a ESP32 S3 MCU when executing the expression " (p-(-10))*dt " with p=85.0 and dt=-1.0
with te_compile 39 times.
Guru Meditation Error: Core 1 panic'ed (Unhandled debug exception).
Debug exception reason: Stack canary watchpoint triggered (loopTask)
Core 1 register dump:
PC : 0x420fd7ab PS : 0x00060536 A0 : 0x820fe4d9 A1 : 0x3fce0ef0
A2 : 0x00000000 A3 : 0x3fcf272d A4 : 0x3fce133c A5 : 0x3c155edc
A6 : 0x00060d23 A7 : 0x00000001 A8 : 0x803803ae A9 : 0x3fce0f70
A10 : 0x3fce2f10 A11 : 0x00000018 A12 : 0x00000009 A13 : 0x00000020
A14 : 0x3fcf272c A15 : 0x00000000 SAR : 0x00000018 EXCCAUSE: 0x00000001
EXCVADDR: 0x00000000 LBEG : 0x400570e8 LEND : 0x400570f3 LCOUNT : 0x00000000
Backtrace:0x420fd7a8:0x3fce0ef00x420fe4d6:0x3fce0fb0 0x4200d50d:0x3fce0fd0 0x4200d953:0x3fce1000 0x4200d98a:0x3fce1030 0x4200d9e6:0x3fce1060 0x4200d6be:0x3fce1090 0x4200d732:0x3fce10c0 0x4200d91a:0x3fce10f0 0x4200d939:0x3fce1120 0x4200d98a:0x3fce1150 0x4200d9e6:0x3fce1180 0x4200d6f4:0x3fce11b0 0x4200d732:0x3fce11e0 0x4200d91a:0x3fce1210 0x4200d939:0x3fce1240 0x4200d98a:0x3fce1270 0x4200d9e6:0x3fce12a0 0x4200d6be:0x3fce12d0 0x4200d732:0x3fce1300 0x4200df55:0x3fce1330 0x42008ebf:0x3fce1380 0x42008f2b:0x3fce1420 0x42008f2b:0x3fce14c0 0x42008f2b:0x3fce1560 0x42008f2b:0x3fce1600 0x42008f2b:0x3fce16a0 0x42008f2b:0x3fce1740 0x42008f2b:0x3fce17e0 0x42008f2b:0x3fce1880 0x42008f2b:0x3fce1920 0x42008f2b:0x3fce19c0 0x42008f2b:0x3fce1a60 0x42008f2b:0x3fce1b00 0x42008f2b:0x3fce1ba0 0x42008f2b:0x3fce1c40 0x42008f2b:0x3fce1ce0 0x42008f2b:0x3fce1d80 0x42008f2b:0x3fce1e20 0x42008f2b:0x3fce1ec0 0x42008f2b:0x3fce1f60 0x42008f2b:0x3fce2000 0x42008f2b:0x3fce20a0 0x42008f2b:0x3fce2140 0x42008f2b:0x3fce21e0 0x42008f2b:0x3fce2280 0x42008f2b:0x3fce2320 0x42008f2b:0x3fce23c0 0x42008f2b:0x3fce2460 0x42008f2b:0x3fce2500 0x42008f2b:0x3fce25a0 0x42008f2b:0x3fce2640 0x42008f2b:0x3fce26e0 0x42008f2b:0x3fce2780 0x42008f2b:0x3fce2820 0x42008f2b:0x3fce28c0 0x42008f2b:0x3fce2960 0x42008f2b:0x3fce2a00 0x42008f2b:0x3fce2aa0 0x42008f2b:0x3fce2b40 0x42008f2b:0x3fce2be0 0x4200ac55:0x3fce2c80 0x420040cd:0x3fce2ce0 0x42021801:0x3fce2da0
double a=this->measureValues[0];
double h=this->measureValues[1];
double pt=this->measureValues[2];
double dt=this->measureValues[3];
double p=this->measureValues[4];
double m=this->measureValues[5];
double t=this->measureValues[6];
double u=this->measureValues[7];
const char* a_n="a";
const char* h_n="h";
const char* pt_n="pt";
const char* dt_n="dt";
const char* p_n="p";
const char* m_n="m";
const char* t_n="t";
const char* u_n="u";
te_variable vars[]= {
{.name=dt_n, .address = &dt},
{.name=p_n, .address = &p}
};
int err;
te_expr *n = te_compile(this->config.custom_maturity_equation.c_str(), vars, numVars, &err);
You're almost certainly doing something wrong with the te_compile or evaluate call. Your example is weird, because you define a bunch of doubles and char pointers, but you only use dt and p. Also, you don't show numVars, but if it's anything more than 2, that could definitely cause a memory error.
Furthermore, you don't need to copy the array to doubles.
This code is equivalent to what you have above:
te_variable vars[]= {
{.name="dt", .address = this->measureValues+3},
{.name="p", .address = this->measureValues+4}
};
int numVars = 2;
int err;
te_expr *n = te_compile(this->config.custom_maturity_equation.c_str(), vars, numVars, &err);
I've done a lot of embed programming myself. I highly suggest you actually get all this stuff working on a PC first. It's much easier to develop, test, and learn in that environment. After all that's good, you can port it to the embedded environment and save yourself a lot of work.
Edit: see my post in the other thread. Your definitely setting numVars incorrectly, which is causing the memory error.