Phase 2: Fix C++ memory leaks and add destructors

Co-authored-by: Genaker <9213670+Genaker@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-02-10 08:48:37 +00:00
parent 45ded9f8a6
commit 7fe3cc1997
3 changed files with 23 additions and 2 deletions

View File

@@ -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);

View File

@@ -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

View File

@@ -89,6 +89,9 @@ class MyServerCallbacks : public BLEServerCallbacks
}
};
// Create static instance to avoid memory leak
MyServerCallbacks serverCallbacks;
#else
#include <NimBLEDevice.h>
@@ -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);