Skip to content

Commit b4abc34

Browse files
authored
fix: memory leak in KernelCPD (#89)
* fix(pelt): memory leak in the C implementation * chore(kernelcpd): remove unused variables * chore: remove debug code
1 parent 8c9aa35 commit b4abc34

3 files changed

Lines changed: 9 additions & 14 deletions

File tree

ruptures/detection/_detection/ekcpd_computation.c

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ static inline int int_min(int a, int b)
2525
*/
2626
void ekcpd_compute(double *signal, int n_samples, int n_dims, int n_bkps, int min_size, void *kernelDescObj, int *M_path)
2727
{
28-
int i, j, t, s, k;
28+
int t, s, k;
2929
int n_bkps_max;
3030

3131
// Allocate memory
@@ -38,19 +38,15 @@ void ekcpd_compute(double *signal, int n_samples, int n_dims, int n_bkps, int mi
3838
S = (double *)malloc((n_samples + 1) * sizeof(double));
3939
M_V = (double *)malloc((n_samples + 1) * (n_bkps + 1) * sizeof(double));
4040

41-
// D and S
42-
for (i = 0; i < (n_samples + 1); i++)
41+
// D, S, M_V and M_path
42+
for (t = 0; t < (n_samples + 1); t++)
4343
{
44-
D[i] = 0.0;
45-
S[i] = 0.0;
46-
}
47-
// M_V and M_path
48-
for (i = 0; i < (n_samples + 1); i++)
49-
{
50-
for (j = 0; j < (n_bkps + 1); j++)
44+
D[t] = 0.0;
45+
S[t] = 0.0;
46+
for (k = 0; k < (n_bkps + 1); k++)
5147
{
52-
M_V[i * (n_bkps + 1) + j] = 0.0;
53-
M_path[i * (n_bkps + 1) + j] = 0;
48+
M_V[t * (n_bkps + 1) + k] = 0.0;
49+
M_path[t * (n_bkps + 1) + k] = 0;
5450
}
5551
}
5652

ruptures/detection/_detection/ekcpd_pelt_computation.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <stdio.h>
21
#include <string.h>
32
#include <stdlib.h>
43
#include <math.h>
@@ -98,6 +97,7 @@ void ekcpd_pelt_compute(double *signal, int n_samples, int n_dims, double beta,
9897
free(D);
9998
free(S);
10099
free(M_V);
100+
free(M_pruning);
101101

102102
return;
103103
}

ruptures/utils/_utils/convert_path_matrix_c.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#include <stdio.h>
21
#include <math.h>
32

43
void convert_path_matrix_c(int *path_matrix, int n_bkps, int n_samples, int n_bkps_max, int jump, int *bkps_list)

0 commit comments

Comments
 (0)