Lossing RAM continuesly while data logging in SD card

#1

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
https://drive.google.com/drive/folders/1g7f2cIiMQ4IpTu7sZRP3qD2xcXH0984X?usp=sharing

#2

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

#3

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.

#4

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.
Ex:

struct app_ctx {
  struct mgos_ds3231 *ds;
  struct mgos_imu *imu;
};
// ...
void imu_cb(void *userdata) {
  struct app_ctx *ctx = (struct app_ctx *) userdata;
  timer_cb(ctx->ds);
  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
#5

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 .
Annotation%202020-01-17%20092047
if not then it start leaking the heap as it does previously.
Can you tell me another solution?
Thanks for the above.

#6

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?

#7

I uncomment it in imu_cb?

#8

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

#9

OK I will remove dt from it.