Fixed memory leak
This commit is contained in:
parent
ca5abf097b
commit
c3fa3d5957
@ -3,8 +3,8 @@
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
#include "esp_system.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
|
||||
@ -86,6 +86,8 @@ Matrix2::Matrix2() {}
|
||||
|
||||
Matrix2::Matrix2(int nRows, int nCols) : nRows(nRows), nCols(nCols) {
|
||||
this->nValues = nRows * nCols;
|
||||
//printf("Allocate %d\n", this->nValues);
|
||||
|
||||
if (this->nValues <= 0) {
|
||||
this->data = nullptr;
|
||||
this->externalData = false;
|
||||
@ -153,8 +155,10 @@ Matrix2& Matrix2::operator=(const Matrix2& m) {
|
||||
}
|
||||
|
||||
Matrix2::~Matrix2() {
|
||||
if (!this->externalData)
|
||||
if (!this->externalData) {
|
||||
//printf("Deallocate %d\n", this->nValues);
|
||||
delete[] data;
|
||||
}
|
||||
}
|
||||
|
||||
Matrix2 Matrix2::Clone() const {
|
||||
@ -165,27 +169,27 @@ Matrix2 Matrix2::Clone() const {
|
||||
}
|
||||
|
||||
// Move constructor
|
||||
Matrix2::Matrix2(Matrix2&& other) noexcept
|
||||
: nRows(other.nRows),
|
||||
nCols(other.nCols),
|
||||
nValues(other.nValues),
|
||||
data(other.data) {
|
||||
other.data = nullptr; // Set the other object's pointer to nullptr to avoid
|
||||
// double deletion
|
||||
}
|
||||
// Matrix2::Matrix2(Matrix2&& other) noexcept
|
||||
// : nRows(other.nRows),
|
||||
// nCols(other.nCols),
|
||||
// nValues(other.nValues),
|
||||
// data(other.data) {
|
||||
// other.data = nullptr; // Set the other object's pointer to nullptr to avoid
|
||||
// // double deletion
|
||||
// }
|
||||
|
||||
// Move assignment operator
|
||||
Matrix2& Matrix2::operator=(Matrix2&& other) noexcept {
|
||||
if (this != &other) {
|
||||
delete[] data; // Clean up current data
|
||||
nRows = other.nRows;
|
||||
nCols = other.nCols;
|
||||
nValues = other.nValues;
|
||||
data = other.data;
|
||||
other.data = nullptr; // Avoid double deletion
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
// Matrix2& Matrix2::operator=(Matrix2&& other) noexcept {
|
||||
// if (this != &other) {
|
||||
// delete[] data; // Clean up current data
|
||||
// nRows = other.nRows;
|
||||
// nCols = other.nCols;
|
||||
// nValues = other.nValues;
|
||||
// data = other.data;
|
||||
// other.data = nullptr; // Avoid double deletion
|
||||
// }
|
||||
// return *this;
|
||||
// }
|
||||
|
||||
Matrix2 Matrix2::Zero(int nRows, int nCols) {
|
||||
Matrix2 r = Matrix2(nRows, nCols);
|
||||
@ -258,7 +262,7 @@ Matrix2 LinearAlgebra::Matrix2::operator+(const Matrix2& v) const {
|
||||
return r;
|
||||
}
|
||||
|
||||
Matrix2 Matrix2::operator+=(const Matrix2& v) {
|
||||
Matrix2& Matrix2::operator+=(const Matrix2& v) {
|
||||
for (int ix = 0; ix < this->nValues; ix++)
|
||||
this->data[ix] += v.data[ix];
|
||||
return *this;
|
||||
@ -359,17 +363,9 @@ void Matrix2::UpdateSlice(int rowStart,
|
||||
int colStart,
|
||||
int colStop,
|
||||
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;
|
||||
for (int rowIx = rowStart; rowIx < rowStop; rowIx++) {
|
||||
rRowDataIx = rowIx * this->nCols;
|
||||
// rRowDataIx += this->nCols;
|
||||
int rRowDataIx = rowIx * this->nCols;
|
||||
mRowDataIx += m.nCols;
|
||||
for (int colIx = colStart; colIx < colStop; colIx++) {
|
||||
this->data[rRowDataIx + colIx] = m.data[mRowDataIx + (colIx - colStart)];
|
||||
|
@ -87,7 +87,7 @@ class Matrix2 {
|
||||
/// @param m The matrix to add to this matrix
|
||||
/// @return The result of the addition
|
||||
Matrix2 operator+(const Matrix2& v) const;
|
||||
Matrix2 operator+=(const Matrix2& v);
|
||||
Matrix2& operator+=(const Matrix2& v);
|
||||
|
||||
Matrix2 operator-(const Matrix2& v) const;
|
||||
|
||||
@ -148,10 +148,11 @@ class Matrix2 {
|
||||
Matrix2 DeleteRows(int rowStart, int rowStop);
|
||||
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
|
||||
Matrix2(Matrix2&& other) noexcept;
|
||||
Matrix2& operator=(Matrix2&& other) noexcept;
|
||||
//Matrix2(Matrix2&& other) noexcept;
|
||||
//Matrix2& operator=(Matrix2&& other) noexcept;
|
||||
|
||||
static Matrix2 Omega(const Vector3& v);
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user