From b4abc340b637b8615c464fda49fddcd6604e5896 Mon Sep 17 00:00:00 2001 From: Charles T Date: Wed, 2 Dec 2020 14:46:11 +0100 Subject: [PATCH] fix: memory leak in KernelCPD (#89) * fix(pelt): memory leak in the C implementation * chore(kernelcpd): remove unused variables * chore: remove debug code --- .../detection/_detection/ekcpd_computation.c | 20 ++++++++----------- .../_detection/ekcpd_pelt_computation.c | 2 +- ruptures/utils/_utils/convert_path_matrix_c.c | 1 - 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/ruptures/detection/_detection/ekcpd_computation.c b/ruptures/detection/_detection/ekcpd_computation.c index e67625a8..a1b91194 100644 --- a/ruptures/detection/_detection/ekcpd_computation.c +++ b/ruptures/detection/_detection/ekcpd_computation.c @@ -25,7 +25,7 @@ static inline int int_min(int a, int b) */ void ekcpd_compute(double *signal, int n_samples, int n_dims, int n_bkps, int min_size, void *kernelDescObj, int *M_path) { - int i, j, t, s, k; + int t, s, k; int n_bkps_max; // Allocate memory @@ -38,19 +38,15 @@ void ekcpd_compute(double *signal, int n_samples, int n_dims, int n_bkps, int mi S = (double *)malloc((n_samples + 1) * sizeof(double)); M_V = (double *)malloc((n_samples + 1) * (n_bkps + 1) * sizeof(double)); - // D and S - for (i = 0; i < (n_samples + 1); i++) + // D, S, M_V and M_path + for (t = 0; t < (n_samples + 1); t++) { - D[i] = 0.0; - S[i] = 0.0; - } - // M_V and M_path - for (i = 0; i < (n_samples + 1); i++) - { - for (j = 0; j < (n_bkps + 1); j++) + D[t] = 0.0; + S[t] = 0.0; + for (k = 0; k < (n_bkps + 1); k++) { - M_V[i * (n_bkps + 1) + j] = 0.0; - M_path[i * (n_bkps + 1) + j] = 0; + M_V[t * (n_bkps + 1) + k] = 0.0; + M_path[t * (n_bkps + 1) + k] = 0; } } diff --git a/ruptures/detection/_detection/ekcpd_pelt_computation.c b/ruptures/detection/_detection/ekcpd_pelt_computation.c index f677988e..d447098d 100644 --- a/ruptures/detection/_detection/ekcpd_pelt_computation.c +++ b/ruptures/detection/_detection/ekcpd_pelt_computation.c @@ -1,4 +1,3 @@ -#include #include #include #include @@ -98,6 +97,7 @@ void ekcpd_pelt_compute(double *signal, int n_samples, int n_dims, double beta, free(D); free(S); free(M_V); + free(M_pruning); return; } diff --git a/ruptures/utils/_utils/convert_path_matrix_c.c b/ruptures/utils/_utils/convert_path_matrix_c.c index bb3a2d6f..6dccfb2c 100644 --- a/ruptures/utils/_utils/convert_path_matrix_c.c +++ b/ruptures/utils/_utils/convert_path_matrix_c.c @@ -1,4 +1,3 @@ -#include #include void convert_path_matrix_c(int *path_matrix, int n_bkps, int n_samples, int n_bkps_max, int jump, int *bkps_list)