Closed adamgarbo closed 2 years ago
In general, begin()
is designed to only be called once.
I haven't been able to find the 72-byte memory monster yet and would appreciate any insight you can offer on how to fix the library.
Might be here: https://github.com/adafruit/Adafruit_LSM6DS/blob/1ea3bd2c77be022ea5c847029471f7bf57635ee6/Adafruit_LSM6DS.cpp#L77-L79
Gets called here: https://github.com/adafruit/Adafruit_LSM6DS/blob/1ea3bd2c77be022ea5c847029471f7bf57635ee6/Adafruit_LSM6DS33.cpp#L37
Try adding in these 3 lines to Adafruit_LSM6DS.cpp
to check and delete if they exist:
if (temp_sensor) delete temp_sensor;
if (accel_sensor) delete accel_sensor;
if (gyro_sensor) delete gyro_sensor;
temp_sensor = new Adafruit_LSM6DS_Temp(this);
accel_sensor = new Adafruit_LSM6DS_Accelerometer(this);
gyro_sensor = new Adafruit_LSM6DS_Gyro(this);
See if that helps?
Hi @caternuson,
Thanks for the quick reply. In low-power applications, it frequently necessary to power down and reinitialize I2C devices. This is actually the first memory leak I've encountered in over 5 years of doing so.
I've added the lines you suggested directly above line 77 of Adafruit_LSM6DS33.cpp
file and it seemed to do the trick.
If you think this would be suitable as a "catch-all" fix, I'd be happy to submit a PR.
Cheers, Adam
Output:
17:41:57.890 -> Initialized LSM6DS33 freeRam(): 27615
17:42:03.964 -> Initialized LSM6DS33 freeRam(): 27615
17:42:09.993 -> Initialized LSM6DS33 freeRam(): 27615
17:47:18.707 -> Initialized LSM6DS33 freeRam(): 27615
17:47:24.737 -> Initialized LSM6DS33 freeRam(): 27615
17:47:30.780 -> Initialized LSM6DS33 freeRam(): 27615
17:47:36.843 -> Initialized LSM6DS33 freeRam(): 27615
17:47:42.893 -> Initialized LSM6DS33 freeRam(): 27615
17:47:48.954 -> Initialized LSM6DS33 freeRam(): 27615
17:47:55.019 -> Initialized LSM6DS33 freeRam(): 27615
17:48:01.045 -> Initialized LSM6DS33 freeRam(): 27615
17:48:07.108 -> Initialized LSM6DS33 freeRam(): 27615
17:48:13.165 -> Initialized LSM6DS33 freeRam(): 27615
17:48:19.225 -> Initialized LSM6DS33 freeRam(): 27615
It'd be interesting to see how other Arduino libraries have handled this scenario. Can you link to a good representational library? That does dynamic memory allocation and works well with deep sleep?
most dont malloc/new @adamgarbo please submit a PR, thanks! :)
@adamgarbo Is this resolved?
Thanks for the reminder. Appears to be resolved by https://github.com/adafruit/Adafruit_LSM6DS/pull/29
Hi @ladyada,
I have been investigating the cause of the most recent version of my Adafruit-powered Cryologger iceberg tracking beacons resetting themselves after 355 hourly samples. After some troubleshooting, I discovered there appears to be a memory leak caused by reinitializations of the LSM6DS33 sensor. Each time the initialization occurs, it consumes 72 bytes of free memory. With the Adafruit Feather M0 Basic Proto - ATSAMD21 it takes approximately 355 initializations for the SRAM to be exhausted and the "KABOOM" to occur. I should also note that reinitializations of the LIS3MDL are not affected by this issue.
I have tested and confirmed this issue on both SAMD21 and SAMD51 microcontrollers using the simplified code example below. I haven't been able to find the 72-byte memory monster yet and would appreciate any insight you can offer on how to fix the library.
Best regards, Adam
Arduino board: Adafruit LSM6DS33 + LIS3MDL - 9 DoF IMU with Accel / Gyro / Mag - STEMMA QT Qwiic
Arduino IDE version: 1.18.13
Steps to reproduce the problem: Please see below.
Example Code:
Feather M0 Proto + LSM6DS33 + LIS3MDL
Output:
SparkFun MicroMod Data Logging Carrier Board + SAMD51 Processor
Output: