Lossing RAM continuesly while data logging in SD card


If you are asking a question, please follow this template:

  1. My goal is: Reading and Logging IMU and RTC data in sd card

  2. My actions are: I used Basic IMU and RTC libraries with sdlib by nilivu

  3. The result I see is: new%20heap
    after ram fills it restarts I don’t want that

  4. My expectation & question is: How to remove this error.

My app is in following link


It looks like you allocate memory and don’t free it (lines 34 and 35 in main.c).


I added mgos_ds3231_free(ds);
on line 39;but same problem new heap is showing up ,nothings changed
can you look again
thank you in advance.


There is another object which you are creating, never used and never freed.
Why do you create the ds3231 object in a timer callback? Create it once in mgos_app_init and pass the pointer to the timer callback.

struct app_ctx {
  struct mgos_ds3231 *ds;
  struct mgos_imu *imu;
// ...
void imu_cb(void *userdata) {
  struct app_ctx *ctx = (struct app_ctx *) userdata;
  struct mgos_imu *imu = ctx->imu;
// ...

// in mgos_app_init
  struct app_ctx *ctx = (struct app_ctx *) calloc(1, sizeof(*ctx));
  ctx->ds = mgos_ds3231_create(addr);
  ctx->imu = imu;
  mgos_set_timer(100, true, imu_cb, ctx);
// ...
1 Like

I added the above logic in my code but still, Losing RAM.
After Ram is empty it stuck in the reboot loop with core dumping procedure.
If i used free(ctx); then it give the abort() and the error is Corrupt heap .
if not then it start leaking the heap as it does previously.
Can you tell me another solution?
Thanks for the above.


Don’t free(ctx). It must leave as long as the application leaves.
Did you remove or comment out struct mgos_ds3231_date_time *dt = mgos_ds3231_date_time_create(); in imu_cb?


I uncomment it in imu_cb?


You don’t use dt anywhere, so remove that line which leaks memory.


OK I will remove dt from it.