Skip to content

Commit

Permalink
Fixed memory leaks in continuous code
Browse files Browse the repository at this point in the history
  • Loading branch information
vcabeli committed Jul 27, 2020
1 parent d219093 commit 72d6ca2
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/compute_ens_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ double* computeEnsInformationContinuous(Environment& environment, int* myCond,
// res_new[0]=(double) samplesNotNA;
}
} // optimal z search
delete[] scoresZ;

if (ziContPosIdx != NULL) delete[] ziContPosIdx;
}
Expand Down
36 changes: 10 additions & 26 deletions src/info_cnt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ void optfun_onerun_kmdl_coarse(vector<int> sortidx_var, vector<int> data, int nb
// optimize on x I(x,y): Hx - Hxy - kmdl
// optimize on y I(x,y): Hy - Hxy - kmdl
// until convergence
double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx,
vector<double> compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx,
vector<int> ptr_cnt, vector<int> ptrVarIdx, vector<int> AllLevels, int n,
int** cut, int* r, vector<double> sample_weights, bool flag_sample_weights,
Environment& environment, bool saveIterations) {
Expand All @@ -355,7 +355,7 @@ double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx
int j, l;

// res_tempults res_temp[0]->I,res_temp[1]->I-k
double* res_temp = (double*)calloc(2, sizeof(double));
vector<double> res_temp;

// allocation factors x y
int** datafactors;
Expand Down Expand Up @@ -459,7 +459,6 @@ double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx
max_initbins = new_initbins;
max_res = res_temp[1];
}
free(res_temp);
}

int lbin = n / max_initbins;
Expand Down Expand Up @@ -610,14 +609,13 @@ double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx
}
MIk[stop] = res_temp[1];
MI[stop] = res_temp[0];
free(res_temp);

if (flag || (ptr_cnt[ptrVarIdx[0]] == 0) || (ptr_cnt[ptrVarIdx[1]] == 0)) {
break;
}
} // for

double* return_res = (double*)calloc(2, sizeof(double));
vector<double> return_res(2);
if (flag) {
return_res[0] = I_av;
return_res[1] = Ik_av;
Expand Down Expand Up @@ -676,7 +674,7 @@ double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx
return return_res;
}

double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
vector<double> compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
vector<vector<int> > sortidx, vector<int> ptr_cnt, vector<int> ptrVarIdx,
vector<int> AllLevels, int nbrUi, int n, int** cut, int* r, int lbin,
vector<double> sample_weights, bool flag_sample_weights,
Expand All @@ -689,7 +687,7 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
int j, l;
int STEPMAX1 = 50;
// res_tempults res_temp[0]->I,res_temp[1]->I-k
double* res_temp = (double*)calloc(2, sizeof(double));
vector<double> res_temp;
// allocation factors x y
int** datafactors = (int**)calloc((nbrUi + 2), sizeof(int*));
for (l = 0; l < (nbrUi + 2); l++) {
Expand Down Expand Up @@ -795,7 +793,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_y_xu = res_temp[0]; // Before optimization on X.
Ik_y_xu = res_temp[1];
free(res_temp);

r_temp[0] = r[0];
r_temp[1] = ruiyx[1];
Expand All @@ -808,7 +805,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_x_yu = res_temp[0]; // Before updating Y (and X).
Ik_x_yu = res_temp[1];
free(res_temp);

if ((Ik_y_xu + Ik_x_yu) > max_res) {
max_initbins = new_initbins;
Expand Down Expand Up @@ -911,7 +907,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_y_xu = res_temp[0]; // Before optimization on X.
Ik_y_xu = res_temp[1];
free(res_temp);
if ((ptr_cnt[ptrVarIdx[0]] == 1) && (r_old[0] > 1)) {
np = min(AllLevels[ptrVarIdx[0]], maxbins);
if (r_old[0] < np) {
Expand Down Expand Up @@ -1009,7 +1004,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_x_yu = res_temp[0]; // Before updating Y (and X).
Ik_x_yu = res_temp[1];
free(res_temp);
if ((ptr_cnt[ptrVarIdx[1]] == 1) && (r_old[1] > 1)) {
np = min(AllLevels[ptrVarIdx[1]], maxbins);
if (r_old[1] < np) {
Expand Down Expand Up @@ -1095,7 +1089,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_x_u = res_temp[0]; // After optimization on U.
Ik_x_u = res_temp[1];
free(res_temp);
// Reset cutpoints on U
reset_u_cutpoints(cut, nbrUi, ptr_cnt, ptrVarIdx, max_initbins, maxbins, lbin,
r, AllLevels, n);
Expand Down Expand Up @@ -1155,7 +1148,6 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
r_temp, n, environment.cache.cterm, 0);
I_y_u = res_temp[0]; // After optimization on U.
Ik_y_u = res_temp[1];
free(res_temp);
// Reset cutpoints on U
reset_u_cutpoints(cut, nbrUi, ptr_cnt, ptrVarIdx, max_initbins, maxbins, lbin,
r, AllLevels, n);
Expand Down Expand Up @@ -1208,7 +1200,7 @@ double* compute_Ixy_cond_u_new_alg1(vector<vector<int> > data,
MI1[stop1] = cond_I;

} // end stop1
double* return_res = (double*)calloc(2, sizeof(double));
vector<double> return_res(2);
if (flag1) {
return_res[0] = I_av1;
return_res[1] = Ik_av1;
Expand Down Expand Up @@ -1258,9 +1250,8 @@ double* compute_mi_cond_alg1(vector<vector<int> > data,
int maxbins = environment.maxbins;
int initbins = environment.initbins;

double* res = new double[3](); // results res[0]->I,res[1]->I-k
double* res_temp; //=(double *)calloc(2,sizeof(double));//results
//res[0]->I,res[1]->I-k
vector<double> res_temp; // res_temp[0]->I,res_temp[1]->I-k
double* res = new double[3]; // res[0]->Rscore,res[1]->I3,res[2]->Ik3

int j, l;

Expand Down Expand Up @@ -1306,7 +1297,6 @@ double* compute_mi_cond_alg1(vector<vector<int> > data,
res[1] = res_temp[0];
res[2] = res_temp[0] - res_temp[1];

free(res_temp);
free(r);
for (l = 0; l < (nbrUi + 2); l++) {
free(cut[l]);
Expand Down Expand Up @@ -1336,7 +1326,6 @@ double* compute_mi_cond_alg1(vector<vector<int> > data,
res[1] = res_temp[0];
res[2] = res_temp[0] - res_temp[1];

free(res_temp);
free(r);
for (l = 0; l < (nbrUi + 2); l++) {
free(cut[l]);
Expand Down Expand Up @@ -1370,9 +1359,8 @@ double* compute_Rscore_Ixyz_alg5(vector<vector<int> > data,
double Ik_xy_u, Ik_xz_u, Ik_yz_u, Ik_xy_zu;
double I_xyz_u, Ik_xyz_u;

double* res_temp =
(double*)calloc(2, sizeof(double)); // results res[0]->I,res[1]->I-k
double* res = new double[3](); // results res[0]->I,res[1]->I-k
vector<double> res_temp; // res_temp[0]->I,res_temp[1]->I-k
double* res = new double[3]; // res[0]->Rscore,res[1]->I3,res[2]->Ik3

vector<int> ptrVarIdx_t((nbrUi + 2));

Expand Down Expand Up @@ -1440,7 +1428,6 @@ double* compute_Rscore_Ixyz_alg5(vector<vector<int> > data,
environment, saveIterations);
I_xy_zu = res_temp[0];
Ik_xy_zu = res_temp[1];
free(res_temp);

// I(x,y|u)
for (l = 0; l < (nbrUi + 2); l++) {
Expand Down Expand Up @@ -1469,7 +1456,6 @@ double* compute_Rscore_Ixyz_alg5(vector<vector<int> > data,
}
I_xy_u = res_temp[0];
Ik_xy_u = res_temp[1];
free(res_temp);

// I(z,x|u)
ptrVarIdx_t[0] = ptrVarIdx[0]; // X
Expand Down Expand Up @@ -1501,7 +1487,6 @@ double* compute_Rscore_Ixyz_alg5(vector<vector<int> > data,
saveIterations);
}
Ik_xz_u = res_temp[1];
free(res_temp);

// I(z,y|u)
ptrVarIdx_t[0] = ptrVarIdx[1]; // Y
Expand Down Expand Up @@ -1533,7 +1518,6 @@ double* compute_Rscore_Ixyz_alg5(vector<vector<int> > data,
saveIterations);
}
Ik_yz_u = res_temp[1];
free(res_temp);

// compute conditional three point mutual information
I_xyz_u = I_xy_u - I_xy_zu;
Expand Down
2 changes: 1 addition & 1 deletion src/info_cnt.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace computation {

using std::vector;

double* compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx,
vector<double> compute_Ixy_alg1(vector<vector<int> > data, vector<vector<int> > sortidx,
vector<int> ptr_cnt, vector<int> ptrVarIdx, vector<int> AllLevels, int n,
int** cut, int* r, vector<double> sample_weights, bool flag_sample_weights,
structure::Environment& environment, bool saveIterations = false);
Expand Down
12 changes: 6 additions & 6 deletions src/mutual_information.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,10 @@ void jointfactors_u(int **datafactors, int *ptrIdx, int n, int Mui, int *r,
}

// rux -> 0:x,1;u,2:ux
double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
vector<double> computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, int n_eff, std::vector<double> sample_weights,
std::shared_ptr<CtermCache> cache, int flag) {
double *I = (double *)calloc(2, sizeof(double));
vector<double> I(2);

int j, x, u, ux;

Expand Down Expand Up @@ -459,9 +459,9 @@ double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
return I;
}

double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
vector<double> computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, std::shared_ptr<CtermCache> cache, int flag) {
double *I = (double *)calloc(2, sizeof(double));
vector<double> I(2);

int j, x, u, ux;

Expand Down Expand Up @@ -510,9 +510,9 @@ double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
}

// rux -> 0:x,1;u,2:ux
double *computeMI_kmdl(int *xfactors, int *ufactors, int *uxfactors, int *rux,
vector<double> computeMI_kmdl(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, std::shared_ptr<CtermCache> cache, int flag) {
double *I = (double *)calloc(2, sizeof(double));
vector<double> I(2);

int j, x, u, ux;

Expand Down
16 changes: 9 additions & 7 deletions src/mutual_information.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
namespace miic {
namespace computation {

using std::vector;

// functions headers of the modules for dynamic programming optimization
// INPUT:
// memory_cuts: vector length n with recorded recursvely best cuts,
Expand All @@ -19,22 +21,22 @@ namespace computation {
// cut: vector with cuts point-> [0 cut[0]][cut[0]+1 cut[1]]...[cut[r-2]
// cut[r-1]] int reconstruction_cut_coarse(int *memory_cuts, int
// *memory_cuts2,int np, int n, int *cut);
int reconstruction_cut_coarse(std::vector<int> &memory_cuts,
std::vector<int> &memory_cuts2, int np, int n, int *cut);
int reconstruction_cut_coarse(vector<int> &memory_cuts,
vector<int> &memory_cuts2, int np, int n, int *cut);
void update_datafactors(
std::vector<std::vector<int> > &sortidx, int varidx,
vector<vector<int> > &sortidx, int varidx,
int** datafactors, int d, int n, int **cut);
// compute jointfactors functions
void jointfactors_uiyx(
int **datafactors, int dui, int n, int Mui, int *r, int **, int *);
void jointfactors_u(int **datafactors, int *ptrIdx, int n, int Mui, int *r,
int *ufactors, int *ru);
double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
vector<double> computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, std::shared_ptr<CtermCache> cache, int flag = 0);
double *computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, int n_eff, std::vector<double> sample_weights,
vector<double> computeMI_knml(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, int n_eff, vector<double> sample_weights,
std::shared_ptr<CtermCache> cache, int flag = 0);
double *computeMI_kmdl(int *xfactors, int *ufactors, int *uxfactors, int *rux,
vector<double> computeMI_kmdl(int *xfactors, int *ufactors, int *uxfactors, int *rux,
int n, std::shared_ptr<CtermCache> cache, int flag = 0);
} // namespace computation
} // namespace miic
Expand Down

0 comments on commit 72d6ca2

Please sign in to comment.