using automatic memory management instead of local malloc()/free() pairs

This commit is contained in:
jkriege2 2022-04-21 21:25:37 +02:00
parent 3357ca4240
commit fbbc814331
4 changed files with 14 additions and 17 deletions

View File

@ -28,6 +28,7 @@
#include <cfloat> #include <cfloat>
#include <stdint.h> #include <stdint.h>
#include <QColor> #include <QColor>
#include <vector>
#include "jkqtcommon/jkqtcommon_imexport.h" #include "jkqtcommon/jkqtcommon_imexport.h"
#include "jkqtcommon/jkqtpmathtools.h" #include "jkqtcommon/jkqtpmathtools.h"
@ -654,17 +655,17 @@ inline void JKQTPImagePlot_array2RGBimage(const T* dbl_in, int width, int height
const T* dbl=dbl_in; const T* dbl=dbl_in;
T* dbllog=nullptr; QVector<T> dbllog;
if (logScale) { if (logScale) {
double logB=log10(logBase); double logB=log10(logBase);
dbllog=static_cast<T*>(malloc(static_cast<size_t>(width)*static_cast<size_t>(height)*sizeof(T))); dbllog.resize(static_cast<size_t>(width)*static_cast<size_t>(height));
//memcpy(dbl, dbl_in, width*height*sizeof(T)); //memcpy(dbl, dbl_in, width*height*sizeof(T));
for (int i=0; i<width*height; i++) { for (int i=0; i<width*height; i++) {
dbllog[i]=log10(dbl_in[i])/logB; dbllog[i]=log10(dbl_in[i])/logB;
} }
min=log10(min)/logB; min=log10(min)/logB;
max=log10(max)/logB; max=log10(max)/logB;
dbl=dbllog; dbl=dbllog.data();
} }
double delta=max-min; double delta=max-min;
@ -865,8 +866,6 @@ inline void JKQTPImagePlot_array2RGBimage(const T* dbl_in, int width, int height
} }
} }
} }
if (logScale) free(dbllog);
} }

View File

@ -367,15 +367,14 @@ int JKQTPColumnOverlayImageEnhanced::getImageColumn() const
void JKQTPColumnOverlayImageEnhanced::draw(JKQTPEnhancedPainter &painter) { void JKQTPColumnOverlayImageEnhanced::draw(JKQTPEnhancedPainter &painter) {
const double* d=parent->getDatastore()->getColumnPointer(imageColumn,0); const double* d=parent->getDatastore()->getColumnPointer(imageColumn,0);
size_t imgSize=parent->getDatastore()->getRows(imageColumn); size_t imgSize=parent->getDatastore()->getRows(imageColumn);
bool* locData=static_cast<bool*>(malloc(imgSize*sizeof(bool))); QVector<bool> locData(imgSize,false);
this->data=locData; this->data=locData.data();
this->Ny= static_cast<int>(imgSize/this->Nx); this->Ny= static_cast<int>(imgSize/this->Nx);
for (size_t i=0; i<imgSize; i++) { for (size_t i=0; i<imgSize; i++) {
locData[i]=(d[i]!=0.0); locData[i]=(d[i]!=0.0);
} }
JKQTPOverlayImageEnhanced::draw(painter); JKQTPOverlayImageEnhanced::draw(painter);
free(locData); this->data=nullptr;
data=nullptr;
} }
bool JKQTPColumnOverlayImageEnhanced::usesColumn(int c) const bool JKQTPColumnOverlayImageEnhanced::usesColumn(int c) const

View File

@ -688,7 +688,7 @@ size_t JKQTPDatastore::copyColumn(size_t old_column, size_t start, size_t stride
{ {
JKQTPColumn old=columns[old_column]; JKQTPColumn old=columns[old_column];
size_t rows=old.getRows(); size_t rows=old.getRows();
double* d=static_cast<double*>(malloc(rows*sizeof(double))); QVector<double> d(rows, 0.0);
double* dd=old.getPointer(0); double* dd=old.getPointer(0);
size_t j=0; size_t j=0;
for (size_t i=start; i<rows; i+=stride) { for (size_t i=start; i<rows; i+=stride) {
@ -696,8 +696,7 @@ size_t JKQTPDatastore::copyColumn(size_t old_column, size_t start, size_t stride
//qDebug()<<old_column<<name<<": "<<j<<i<<d[j]; //qDebug()<<old_column<<name<<": "<<j<<i<<d[j];
j++; j++;
} }
size_t n=addCopiedColumn(d, j, name); size_t n=addCopiedColumn(d.data(), j, name);
free(d);
return n; return n;
} }