Fixed memory leak

This commit is contained in:
Pascal Serrarens 2025-07-03 12:12:22 +02:00
parent ca5abf097b
commit c3fa3d5957
2 changed files with 32 additions and 35 deletions

View File

@ -3,8 +3,8 @@
#include <iostream> #include <iostream>
#endif #endif
#include "esp_system.h"
#include "esp_log.h" #include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/task.h" #include "freertos/task.h"
@ -86,6 +86,8 @@ Matrix2::Matrix2() {}
Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) { Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) {
this->nValues = nRows * nCols; this->nValues = nRows * nCols;
//printf("Allocate %d\n", this->nValues);
if (this->nValues <= 0) { if (this->nValues <= 0) {
this->data = nullptr; this->data = nullptr;
this->externalData = false; this->externalData = false;
@ -153,9 +155,11 @@ Matrix2& Matrix2::operator=(const Matrix2& m) {
} }
Matrix2::~Matrix2() { Matrix2::~Matrix2() {
if (!this->externalData) if (!this->externalData) {
//printf("Deallocate %d\n", this->nValues);
delete[] data; delete[] data;
} }
}
Matrix2 Matrix2::Clone() const { Matrix2 Matrix2::Clone() const {
Matrix2 r = Matrix2(this->nRows, this->nCols); Matrix2 r = Matrix2(this->nRows, this->nCols);
@ -165,27 +169,27 @@ Matrix2 Matrix2::Clone() const {
} }
// Move constructor // Move constructor
Matrix2::Matrix2(Matrix2&& other) noexcept // Matrix2::Matrix2(Matrix2&& other) noexcept
: nRows(other.nRows), // : nRows(other.nRows),
nCols(other.nCols), // nCols(other.nCols),
nValues(other.nValues), // nValues(other.nValues),
data(other.data) { // data(other.data) {
other.data = nullptr; // Set the other object's pointer to nullptr to avoid // other.data = nullptr; // Set the other object's pointer to nullptr to avoid
// double deletion // // double deletion
} // }
// Move assignment operator // Move assignment operator
Matrix2& Matrix2::operator=(Matrix2&& other) noexcept { // Matrix2& Matrix2::operator=(Matrix2&& other) noexcept {
if (this != &other) { // if (this != &other) {
delete[] data; // Clean up current data // delete[] data; // Clean up current data
nRows = other.nRows; // nRows = other.nRows;
nCols = other.nCols; // nCols = other.nCols;
nValues = other.nValues; // nValues = other.nValues;
data = other.data; // data = other.data;
other.data = nullptr; // Avoid double deletion // other.data = nullptr; // Avoid double deletion
} // }
return *this; // return *this;
} // }
Matrix2 Matrix2::Zero(int nRows, int nCols) { Matrix2 Matrix2::Zero(int nRows, int nCols) {
Matrix2 r = Matrix2(nRows, nCols); Matrix2 r = Matrix2(nRows, nCols);
@ -258,7 +262,7 @@ Matrix2 LinearAlgebra::Matrix2::operator+(const Matrix2& v) const {
return r; return r;
} }
Matrix2 Matrix2::operator+=(const Matrix2& v) { Matrix2& Matrix2::operator+=(const Matrix2& v) {
for (int ix = 0; ix < this->nValues; ix++) for (int ix = 0; ix < this->nValues; ix++)
this->data[ix] += v.data[ix]; this->data[ix] += v.data[ix];
return *this; return *this;
@ -359,17 +363,9 @@ void Matrix2::UpdateSlice(int rowStart,
int colStart, int colStart,
int colStop, int colStop,
const Matrix2& m) const { const Matrix2& m) const {
// for (int i = rowStart; i < rowStop; i++) {
// for (int j = colStart; j < colStop; j++)
// this->data[i * this->nCols + j] =
// m.data[(i - rowStart) * m.nCols + (j - colStart)];
// }
int rRowDataIx = rowStart * this->nCols;
int mRowDataIx = 0; int mRowDataIx = 0;
for (int rowIx = rowStart; rowIx < rowStop; rowIx++) { for (int rowIx = rowStart; rowIx < rowStop; rowIx++) {
rRowDataIx = rowIx * this->nCols; int rRowDataIx = rowIx * this->nCols;
// rRowDataIx += this->nCols;
mRowDataIx += m.nCols; mRowDataIx += m.nCols;
for (int colIx = colStart; colIx < colStop; colIx++) { for (int colIx = colStart; colIx < colStop; colIx++) {
this->data[rRowDataIx + colIx] = m.data[mRowDataIx + (colIx - colStart)]; this->data[rRowDataIx + colIx] = m.data[mRowDataIx + (colIx - colStart)];

View File

@ -87,7 +87,7 @@ class Matrix2 {
/// @param m The matrix to add to this matrix /// @param m The matrix to add to this matrix
/// @return The result of the addition /// @return The result of the addition
Matrix2 operator+(const Matrix2& v) const; Matrix2 operator+(const Matrix2& v) const;
Matrix2 operator+=(const Matrix2& v); Matrix2& operator+=(const Matrix2& v);
Matrix2 operator-(const Matrix2& v) const; Matrix2 operator-(const Matrix2& v) const;
@ -148,10 +148,11 @@ class Matrix2 {
Matrix2 DeleteRows(int rowStart, int rowStop); Matrix2 DeleteRows(int rowStart, int rowStop);
Matrix2 DeleteColumns(int colStart, int colStop); Matrix2 DeleteColumns(int colStart, int colStop);
// private: // We don't want these because they make impliciti copies which is inefficient
// move constructor and move assignment operator // move constructor and move assignment operator
Matrix2(Matrix2&& other) noexcept; //Matrix2(Matrix2&& other) noexcept;
Matrix2& operator=(Matrix2&& other) noexcept; //Matrix2& operator=(Matrix2&& other) noexcept;
static Matrix2 Omega(const Vector3& v); static Matrix2 Omega(const Vector3& v);