diff --git a/lib/charts/StackedChart.cpp b/lib/charts/StackedChart.cpp index 2c6ca73..21684eb 100644 --- a/lib/charts/StackedChart.cpp +++ b/lib/charts/StackedChart.cpp @@ -10,7 +10,9 @@ size_t StackedChart::addChart(Chart *c) Chart **cc = new Chart *[charts_sz + 1]; memcpy(cc, charts, charts_sz * sizeof(Chart *)); cc[charts_sz] = c; - free(charts); + + // Use delete[] instead of free since we allocated with new[] + delete[] charts; c->reset(pos_x + c->pos_x, pos_y + c->pos_y, trim_w(c->pos_x, c->width, width), c->height); diff --git a/lib/charts/charts.h b/lib/charts/charts.h index 12497e8..912dae8 100644 --- a/lib/charts/charts.h +++ b/lib/charts/charts.h @@ -80,6 +80,13 @@ struct BarChart : ProgressChart, Listener memset(changed, 0, w * sizeof(bool)); }; + ~BarChart() + { + // Clean up dynamically allocated arrays + delete[] ys; + delete[] changed; + } + void reset(uint16_t x, uint16_t y, uint16_t w, uint16_t h) override; void clear(); int updatePoint(float x, float y) override; @@ -122,6 +129,15 @@ struct StackedChart : Chart, Listener StackedChart(Display_t &d, uint16_t x, uint16_t y, uint16_t w, uint16_t h) : Chart(d, x, y, w, h), charts(NULL), charts_sz(0) {}; + ~StackedChart() + { + // Clean up dynamically allocated chart array + if (charts != NULL) + { + delete[] charts; + } + } + /* * addChart adds c to the StackedChart, treats pos_x and pos_y of the chart * as relative to this chart's origin, and trims width to fit. Adjust the diff --git a/src/main.cpp b/src/main.cpp index 9330205..ba5159c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -89,6 +89,9 @@ class MyServerCallbacks : public BLEServerCallbacks } }; +// Create static instance to avoid memory leak +MyServerCallbacks serverCallbacks; + #else #include @@ -242,7 +245,7 @@ void initBT() #else BLEDevice::init("ESP32_RADAR"); pServer = BLEDevice::createServer(); - pServer->setCallbacks(new MyServerCallbacks()); + pServer->setCallbacks(&serverCallbacks); // Use static instance instead of new BLEService *pService = pServer->createService(SERVICE_UUID);