diff --git a/imagery/i.pca/main.c b/imagery/i.pca/main.c index 427cf5f8730..8cc6c4f58fe 100644 --- a/imagery/i.pca/main.c +++ b/imagery/i.pca/main.c @@ -314,6 +314,7 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev, DCELL **rowbuf = (DCELL **)G_malloc(bands * sizeof(DCELL *)); double **sum2 = (double **)G_calloc(bands, sizeof(double *)); double *sumsq, *sd, *sum; + int ret = 1; if (stddev) { sumsq = (double *)G_calloc(bands, sizeof(double)); @@ -358,8 +359,10 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev, } G_percent(1, 1, 1); - if (count < 2) - return 0; + if (count < 2) { + ret = 0; + goto free_exit; + } for (i = 0; i < bands; i++) { if (stddev) { @@ -378,22 +381,21 @@ static int calc_mu_cov(int *fds, double **covar, double *mu, double *stddev, if (j != i) covar[j][i] = covar[i][j]; } - - G_free(sum2[i]); - G_free(rowbuf[i]); } for (i = 0; i < bands; i++) mu[i] = sum[i] / count; +free_exit: + for (i = 0; i < bands; i++) { + G_free(sum2[i]); + G_free(rowbuf[i]); + } G_free(rowbuf); - G_free(sum2); - if (sd) - G_free(sd); - if (sumsq) - G_free(sumsq); + G_free(sd); + G_free(sumsq); - return 1; + return ret; } static int write_pca(double **eigmat, double *mu, double *stddev, int *inp_fd, @@ -571,6 +573,8 @@ static int write_pca(double **eigmat, double *mu, double *stddev, int *inp_fd, G_free(min); G_free(max); G_free(old_range); + G_free(pcs); + G_free(out_fd); return 0; }