Skip to content

Commit df295fb

Browse files
committed
fix memory leaks
applied fastlib#55
1 parent 49cb5a9 commit df295fb

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
11
*.DS_Store
22
__pycache__
3+
.venv/
4+
*.egg-info
5+
*.so
6+
*.wis

src/fcwt/fcwt.cpp

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ limitations under the License.
3737
*/
3838

3939
#include "fcwt.h"
40+
#include <cassert>
41+
#include <cstring>
4042

4143
Morlet::Morlet(float bandwidth) {
4244
four_wavelen = 0.9876f;
@@ -86,8 +88,8 @@ void Morlet::generate(float* real, float* imag, int size, float scale) {
8688
void Morlet::getWavelet(float scale, complex<float>* pwav, int pn) {
8789
int w = getSupport(scale);
8890

89-
float *real = (float*)malloc(sizeof(float)*max(w*2+1,pn));
90-
float *imag = (float*)malloc(sizeof(float)*max(w*2+1,pn));
91+
float *real = (float*)fftwf_malloc(sizeof(float)*max(w*2+1,pn));
92+
float *imag = (float*)fftwf_malloc(sizeof(float)*max(w*2+1,pn));
9193
for(int t=0; t < max(w*2+1,pn); t++) {
9294
real[t] = 0;
9395
imag[t] = 0;
@@ -100,8 +102,8 @@ void Morlet::getWavelet(float scale, complex<float>* pwav, int pn) {
100102
pwav[t].imag(imag[t]);
101103
}
102104

103-
delete real;
104-
delete imag;
105+
fftwf_free(real);
106+
fftwf_free(imag);
105107
};
106108

107109
//==============================================================//
@@ -124,6 +126,10 @@ Scales::Scales(Wavelet *wav, SCALETYPE st, int afs, float af0, float af1, int af
124126

125127
}
126128

129+
Scales::~Scales() {
130+
free(scales);
131+
}
132+
127133
void Scales::getScales(float *pfreqs, int pnf) {
128134
for(int i=0;i<pnf;i++) {
129135
pfreqs[i]=scales[i];
@@ -321,7 +327,7 @@ void FCWT::create_FFT_optimization_plan(int maxsize, int flags) {
321327
for(int i=11; i<=nt; i++) {
322328
int n = 1 << i;
323329

324-
float *dat = (float*)malloc(sizeof(float)*n);
330+
float *dat = (float*)fftwf_malloc(sizeof(float)*n);
325331
fftwf_complex *O1 = fftwf_alloc_complex(n);
326332
fftwf_complex *out = fftwf_alloc_complex(n);
327333

@@ -347,9 +353,11 @@ void FCWT::create_FFT_optimization_plan(int maxsize, int flags) {
347353

348354
fftwf_export_wisdom_to_filename(file_for);
349355

350-
free(dat);
356+
fftwf_free(dat);
351357
fftwf_free(O1);
352358
fftwf_free(out);
359+
fftwf_destroy_plan(p_for);
360+
fftwf_destroy_plan(p_back);
353361

354362
std::cout << "Optimization schemes for N: " << n << " have been calculated. Next time you use fCWT it will automatically choose the right optimization scheme based on number of threads and signal length." << std::endl;
355363
}
@@ -406,6 +414,11 @@ void FCWT::convolve(fftwf_plan p, fftwf_complex *Ihat, fftwf_complex *O1, comple
406414
fftbased(p, Ihat, O1, (float*)lastscalemem, wav->mother, newsize, scale, wav->imag_frequency, wav->doublesided);
407415
if(use_normalization) fft_normalize((complex<float>*)lastscalemem, newsize);
408416
memcpy(out, (complex<float>*)lastscalemem, sizeof(complex<float>)*size);
417+
#ifdef _WIN32
418+
_aligned_free(lastscalemem);
419+
#else
420+
free(lastscalemem);
421+
#endif
409422
} else {
410423
if(!out) {
411424
std::cout << "OUT NOT A POINTER" << std::endl;

src/fcwt/fcwt.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class Morlet : public Wavelet {
9696
class Scales {
9797
public:
9898
FCWT_LIBRARY_API Scales(Wavelet *pwav, SCALETYPE st, int fs, float f0, float f1, int fn);
99+
FCWT_LIBRARY_API ~Scales();
99100

100101
void FCWT_LIBRARY_API getScales(float *pfreqs, int pnf);
101102
void FCWT_LIBRARY_API getFrequencies(float *pfreqs, int pnf);
@@ -164,4 +165,4 @@ inline int find2power(int n)
164165
return(m);
165166
}
166167

167-
#endif
168+
#endif

0 commit comments

Comments
 (0)