Skip to content

Commit

Permalink
Fixed some memory leaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamarituc committed May 21, 2020
1 parent 03c10f5 commit 14f3386
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 18 deletions.
6 changes: 2 additions & 4 deletions dsmin.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@ int dsmin(double **p,double *y,int n,double ftol,double (*func)(double *))
double *vector_sum(double **,int);
double dsmod(double **,double *,double *,int,double (*func)(double *),int,double);

// Allocate memory
psum=(double *) malloc(sizeof(double) * n);

// Get function values
for (i=0;i<=n;i++)
y[i]=func(p[i]);
Expand Down Expand Up @@ -69,7 +66,8 @@ int dsmin(double **p,double *y,int n,double ftol,double (*func)(double *))
}
}
nfunk+=n;


free(psum);
psum=vector_sum(p,n);
}
} else --nfunk;
Expand Down
2 changes: 2 additions & 0 deletions rffft.c
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ int main(int argc,char *argv[])

// Deallocate
free(ibuf);
free(cbuf);
free(fbuf);
fftwf_free(c);
fftwf_free(d);
free(z);
Expand Down
13 changes: 6 additions & 7 deletions rffind.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ int main(int argc,char *argv[])

// Filter
filter(s,site_id,sigma,filename,graves);

// Free
free_spectrogram(s);
}
} else {
// Read data
Expand All @@ -222,14 +225,10 @@ int main(int argc,char *argv[])
// Filter
filter(s,site_id,sigma,filename,graves);
}
}

// Free
free(s.z);
free(s.mjd);
// free(s.zavg);
// free(s.zstd);
// free(s.length);
// Free
free_spectrogram(s);
}

return 0;
}
Expand Down
9 changes: 9 additions & 0 deletions rfio.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,12 @@ void write_spectrogram(struct spectrogram s,char *prefix)

return;
}

void free_spectrogram(struct spectrogram s)
{
free(s.z);
free(s.zavg);
free(s.zstd);
free(s.mjd);
free(s.length);
}
1 change: 1 addition & 0 deletions rfio.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ struct spectrogram {
};
struct spectrogram read_spectrogram(char *prefix,int isub,int nsub,double f0,double df0,int nbin,double foff);
void write_spectrogram(struct spectrogram s,char *prefix);
void free_spectrogram(struct spectrogram s);
1 change: 1 addition & 0 deletions rfplot.c
Original file line number Diff line number Diff line change
Expand Up @@ -722,6 +722,7 @@ int main(int argc,char *argv[])
free(s.zavg);
free(s.zstd);
free(s.mjd);
free(s.length);
if (tf.n>0) {
free(tf.mjd);
free(tf.freq);
Expand Down
3 changes: 0 additions & 3 deletions rfpng.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,9 +254,6 @@ int main(int argc,char *argv[])
free(t[i].freq);
free(t[i].za);
}
// free(tf.mjd);
// free(tf.freq);
// free(tf.za);

return 0;
}
Expand Down
10 changes: 10 additions & 0 deletions simplex.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,13 @@ double **simplex(int n,double *a,double *da)
return p;
}

void simplex_free(double **p,int n)
{
int i;

if(p==NULL)
return;
for(i=0;i<=n;i++)
free(p[i]);
free(p);
}
15 changes: 11 additions & 4 deletions versafit.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ int ERRCOMP=0; // Set reduced Chi-Squared to unity (1 = yes; 0 = no)

int dsmin(double **,double *,int,double,double (*func)(double *));
double **simplex(int,double *,double *);
void simplex_free(double **,int);
double parabolic_root(double,double,double,double);

// Versafit fitting routine
Expand Down Expand Up @@ -51,6 +52,8 @@ void versafit(int m,int n,double *a,double *da,double (*func)(double *),double d
a[i]/=(double) (n+1);
}

simplex_free(p,n);

// Compute minimum
chisqmin=func(a);

Expand Down Expand Up @@ -109,6 +112,8 @@ void versafit(int m,int n,double *a,double *da,double (*func)(double *),double d
}
d[0]=parabolic_root(d[0],func(b),chisqmin,dchisq);

simplex_free(p,n);

if (fabs(chisqmin+dchisq-func(b))<tol) break;
}

Expand All @@ -131,23 +136,25 @@ void versafit(int m,int n,double *a,double *da,double (*func)(double *),double d
}
d[1]=parabolic_root(d[1],func(b),chisqmin,dchisq);

simplex_free(p,n);

if (fabs(chisqmin+dchisq-func(b))<tol) break;
}
da[i]=0.5*(fabs(d[0])+fabs(d[1]));
if (ERRCOMP) da[i]*=errcomp;
}
}

free(b);
free(db);

if (OUTPUT)
for (i=0;i<n;i++)
printf(" a(%i): %12.5f +- %9.5f\n",i+1,a[i],da[i]);
}
if (OUTPUT) printf("\nTotal number of iterations: %i\n",nfunk);

// free(p);
// free(y);
// free(b);
// free(db);
free(y);

return;
}
Expand Down

0 comments on commit 14f3386

Please sign in to comment.