Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix memory leaks #55

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 13 additions & 8 deletions src/fcwt/fcwt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ void Morlet::generate(float* real, float* imag, int size, float scale) {
void Morlet::getWavelet(float scale, complex<float>* pwav, int pn) {
int w = getSupport(scale);

float *real = (float*)malloc(sizeof(float)*max(w*2+1,pn));
float *imag = (float*)malloc(sizeof(float)*max(w*2+1,pn));
float *real = (float*)fftwf_malloc(sizeof(float)*max(w*2+1,pn));
float *imag = (float*)fftwf_malloc(sizeof(float)*max(w*2+1,pn));
for(int t=0; t < max(w*2+1,pn); t++) {
real[t] = 0;
imag[t] = 0;
Expand All @@ -99,9 +99,9 @@ void Morlet::getWavelet(float scale, complex<float>* pwav, int pn) {
pwav[t].real(real[t]);
pwav[t].imag(imag[t]);
}
delete real;
delete imag;

fftwf_free(real);
fftwf_free(imag);
};

//==============================================================//
Expand All @@ -121,7 +121,9 @@ Scales::Scales(Wavelet *wav, SCALETYPE st, int afs, float af0, float af1, int af
calculate_linscale_array(wav->four_wavelen, afs, af0, af1, afn);
else
calculate_linfreq_array(wav->four_wavelen, afs, af0, af1, afn);

}
Scales::~Scales(){
free(scales);
}

void Scales::getScales(float *pfreqs, int pnf) {
Expand Down Expand Up @@ -321,7 +323,7 @@ void FCWT::create_FFT_optimization_plan(int maxsize, int flags) {
for(int i=11; i<=nt; i++) {
int n = 1 << i;

float *dat = (float*)malloc(sizeof(float)*n);
float *dat = (float*)fftwf_malloc(sizeof(float)*n);
fftwf_complex *O1 = fftwf_alloc_complex(n);
fftwf_complex *out = fftwf_alloc_complex(n);

Expand All @@ -347,9 +349,11 @@ void FCWT::create_FFT_optimization_plan(int maxsize, int flags) {

fftwf_export_wisdom_to_filename(file_for);

free(dat);
fftwf_free(dat);
fftwf_free(O1);
fftwf_free(out);
fftwf_destroy_plan(p_for);
fftwf_destroy_plan(p_back);

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;
}
Expand Down Expand Up @@ -406,6 +410,7 @@ void FCWT::convolve(fftwf_plan p, fftwf_complex *Ihat, fftwf_complex *O1, comple
fftbased(p, Ihat, O1, (float*)lastscalemem, wav->mother, newsize, scale, wav->imag_frequency, wav->doublesided);
if(use_normalization) fft_normalize((complex<float>*)lastscalemem, newsize);
memcpy(out, (complex<float>*)lastscalemem, sizeof(complex<float>)*size);
free(lastscalemem);
} else {
if(!out) {
std::cout << "OUT NOT A POINTER" << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions src/fcwt/fcwt.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Morlet : public Wavelet {
class Scales {
public:
FCWT_LIBRARY_API Scales(Wavelet *pwav, SCALETYPE st, int fs, float f0, float f1, int fn);

~Scales();
void FCWT_LIBRARY_API getScales(float *pfreqs, int pnf);
void FCWT_LIBRARY_API getFrequencies(float *pfreqs, int pnf);

Expand Down Expand Up @@ -174,4 +174,4 @@ inline int find2power(int n)
return(m);
}

#endif
#endif