Skip to content

Commit 9f5e6c4

Browse files
committed
[FIX] fix various mem leaks
1 parent 5492e57 commit 9f5e6c4

File tree

10 files changed

+95
-60
lines changed

10 files changed

+95
-60
lines changed

.github/workflows/tests.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,10 @@ jobs:
5959

6060
- name: Run with Valgrind
6161
run: |
62+
# we use definite,indirect (and not possible) to not error on common omp errors
6263
valgrind --leak-check=full \
63-
--show-leak-kinds=all \
64-
--errors-for-leak-kinds=all \
64+
--show-leak-kinds=definite,indirect \
65+
--errors-for-leak-kinds=definite,indirect \
6566
--error-exitcode=1 \
6667
${{ steps.strings.outputs.build-output-dir }}/ADOL-C/boost-test/boost-test-adolc
6768

ADOL-C/boost-test/sparse/sparse_abs_normal.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,8 @@ template <size_t Version> static void problem() {
445445
BOOST_TEST(crs[row][col] == anfProblemAlloc.crs[row][col]);
446446
}
447447
};
448+
for (auto& c: crs)
449+
delete[] c;
448450
}
449451

450452
BOOST_AUTO_TEST_CASE(SparseANFPatTest) {

ADOL-C/boost-test/sparse/sparse_hessian.cpp

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ using ADOLC::Sparse::RecoveryMethod;
2020
identifies and recovers only the diagonal nonzero entries.
2121
2222
The function is:
23-
f(x) = Σ_{i=0}^{4} [ sin(x_i) + (x_{i+10})^2 ]
23+
f(x) = sum_{i=0}^{4} [ sin(x_i) + (x_{i+10})^2 ]
2424
2525
Analytical Hessian (expected):
2626
For i = 0,... , 4:
@@ -106,7 +106,6 @@ static void testSparseHessWithDiagonal(short tapeId) {
106106
// we only have diagonal elements
107107
BOOST_TEST(rowIndices[i] == columnIndices[i]);
108108
}
109-
110109
// test with rowIndices, columnIndices and sparseValues != nullptr
111110
ADOLC::Sparse::sparse_hess<CFM, RCM>(tapeId, dimIn, 0, in.data(),
112111
&numberOfNonzeros, &rowIndices,
@@ -127,7 +126,6 @@ static void testSparseHessWithDiagonal(short tapeId) {
127126
// we only have diagonal elements
128127
BOOST_TEST(rowIndices[i] == columnIndices[i]);
129128
}
130-
131129
// test with repeat == 1
132130
ADOLC::Sparse::sparse_hess<CFM, RCM>(tapeId, dimIn, 1, in.data(),
133131
&numberOfNonzeros, &rowIndices,
@@ -148,7 +146,9 @@ static void testSparseHessWithDiagonal(short tapeId) {
148146
// we only have diagonal elements
149147
BOOST_TEST(rowIndices[i] == columnIndices[i]);
150148
}
151-
delete sparseValues;
149+
delete[] sparseValues;
150+
delete[] columnIndices;
151+
delete[] rowIndices;
152152
for (auto &h : hess)
153153
delete[] h;
154154
}
@@ -164,7 +164,6 @@ BOOST_AUTO_TEST_CASE(SparseHessSafeIndirect) {
164164
testSparseHessWithDiagonal<ControlFlowMode::Safe, RecoveryMethod::Indirect>(
165165
tapeId);
166166
}
167-
168167
BOOST_AUTO_TEST_CASE(SparseHessTightDirect) {
169168
const short tapeId = 634;
170169
testSparseHessWithDiagonal<ControlFlowMode::Tight, RecoveryMethod::Direct>(
@@ -200,7 +199,6 @@ BOOST_AUTO_TEST_CASE(SparseHessOldTightIndirect) {
200199
testSparseHessWithDiagonal<ControlFlowMode::OldTight,
201200
RecoveryMethod::Indirect>(tapeId);
202201
}
203-
204202
/*
205203
This test constructs a function with mixed-product terms so that the Hessian
206204
contains off-diagonal nonzeros.
@@ -314,15 +312,15 @@ static void testSparseHessWithOffDiagonals(short tapeId) {
314312
delete[] rowIndices;
315313
delete[] columnIndices;
316314
delete[] sparseValues;
317-
for (auto &ptr : hess)
318-
delete[] ptr;
315+
for (auto &h: hess)
316+
delete[] h;
319317
}
320-
321318
BOOST_AUTO_TEST_CASE(SparseHessNonDiagSafeDirect) {
322319
const short tapeId = 700;
323320
testSparseHessWithOffDiagonals<ControlFlowMode::Safe, RecoveryMethod::Direct>(
324321
tapeId);
325322
}
323+
326324
BOOST_AUTO_TEST_CASE(SparseHessNonDiagSafeIndirect) {
327325
const short tapeId = 701;
328326
testSparseHessWithOffDiagonals<ControlFlowMode::Safe,
@@ -435,6 +433,9 @@ static void testSparseHessPatWithDiagonal(short tapeId) {
435433
BOOST_TEST(compressedRowStorage[17][0] == 0);
436434
BOOST_TEST(compressedRowStorage[18][0] == 0);
437435
BOOST_TEST(compressedRowStorage[19][0] == 0);
436+
437+
for (auto& crs: compressedRowStorage)
438+
delete[] crs;
438439
}
439440

440441
BOOST_AUTO_TEST_CASE(SparseHessPatDiagSafe) {
@@ -519,6 +520,9 @@ static void testSparseHessPatWithOffDiagonal(short tapeId) {
519520
// sixth hessian row has fifth
520521
BOOST_TEST(compressedRowStorage[5][0] == 1);
521522
BOOST_TEST(compressedRowStorage[5][1] == 4);
523+
524+
for (auto& crs: compressedRowStorage)
525+
delete[] crs;
522526
}
523527

524528
BOOST_AUTO_TEST_CASE(SparseHessPatOffDiagSafe) {
@@ -540,5 +544,4 @@ BOOST_AUTO_TEST_CASE(SparseHessPatOffDiagOldTight) {
540544
const short tapeId = 715;
541545
testSparseHessPatWithOffDiagonal<ControlFlowMode::OldTight>(tapeId);
542546
}
543-
544-
BOOST_AUTO_TEST_SUITE_END()
547+
BOOST_AUTO_TEST_SUITE_END()

ADOL-C/boost-test/sparse/sparse_jacobian.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
#include <algorithm>
32
#include <boost/test/tools/old/interface.hpp>
43
#define BOOST_TEST_DYN_LINK
@@ -94,6 +93,9 @@ static void testSparseJac(short tapeId) {
9493
}
9594
}
9695
}
96+
delete[] rowIndices;
97+
delete[] columnIndices;
98+
delete[] nonzeroValues;
9799
for (auto &j : jac)
98100
delete[] j;
99101
}
@@ -205,6 +207,9 @@ static void testSparseJacPat(short tapeId) {
205207
BOOST_TEST(compressedRowStorage[1][1] == 2);
206208
BOOST_TEST(compressedRowStorage[1][2] == 5);
207209
BOOST_TEST(compressedRowStorage[1][3] == 6);
210+
211+
for (auto& crs: compressedRowStorage)
212+
delete[] crs;
208213
}
209214

210215
BOOST_AUTO_TEST_CASE(SparseJacPatIndexSafe) {
@@ -226,5 +231,4 @@ BOOST_AUTO_TEST_CASE(SparseJacPatBitPatternTight) {
226231
short tapeId = 578;
227232
testSparseJacPat<SparseMethod::BitPattern, ControlFlowMode::Tight>(tapeId);
228233
}
229-
230234
BOOST_AUTO_TEST_SUITE_END()

ADOL-C/include/adolc/sparse/sparsedrivers.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ int computeSparseJac(short tag, int depen, int indep, const double *basepoint,
772772
double **values) {
773773
ValueTape &tape = findTape(tag);
774774
int ret_val = 0;
775+
myfree2(tape.sJInfos().B_);
776+
myfree1(tape.sJInfos().y_);
775777
tape.sJInfos().B_ =
776778
myalloc2(tape.sJInfos().seedRows_, tape.sJInfos().seedClms_);
777779
tape.sJInfos().y_ = myalloc1(depen);
@@ -799,9 +801,9 @@ int computeSparseJac(short tag, int depen, int indep, const double *basepoint,
799801
// everything is preallocated, we assume correctly
800802
// call usermem versions
801803
if (CM == CompressionMode::Row)
802-
tape.sJInfos().recoverRowFormat(rind, cind, values);
804+
tape.sJInfos().recoverRowFormatUserMem(rind, cind, values);
803805
else if (CM == CompressionMode::Column)
804-
tape.sJInfos().recoverColFormat(rind, cind, values);
806+
tape.sJInfos().recoverColFormatUserMem(rind, cind, values);
805807
} else {
806808
// at least one of rind cind values is not allocated, deallocate others
807809
// and call unmanaged versions
@@ -1061,23 +1063,28 @@ int buildHessPatternAndSeed(short tag, int indep, const double *basepoint,
10611063

10621064
// compute seed matrix => ColPack library
10631065

1066+
// Seed is handled by ColPack
10641067
double **Seed = nullptr;
10651068
tape.sHInfos().initColoring(indep);
10661069
if constexpr (RCM == RecoveryMethod::Indirect)
10671070
tape.sHInfos().generateSeedHess(&Seed, "ACYCLIC_FOR_INDIRECT_RECOVERY");
10681071
else if (RCM == RecoveryMethod::Direct)
10691072
tape.sHInfos().generateSeedHess(&Seed, "STAR");
10701073

1074+
// data might still be allocated, ensure that its not leaked
1075+
myfree2(tape.sHInfos().Hcomp_);
1076+
myfree3(tape.sHInfos().Xppp_);
1077+
myfree3(tape.sHInfos().Yppp_);
1078+
myfree3(tape.sHInfos().Zppp_);
1079+
myfree2(tape.sHInfos().Upp_);
1080+
10711081
tape.sHInfos().Hcomp_ = myalloc2(indep, tape.sHInfos().p_);
10721082
tape.sHInfos().Xppp_ = myalloc3(indep, tape.sHInfos().p_, 1);
10731083

10741084
for (int i = 0; i < indep; i++)
10751085
for (int l = 0; l < tape.sHInfos().p_; l++)
10761086
tape.sHInfos().Xppp_[i][l][0] = Seed[i][l];
10771087

1078-
// Seed will be freed by ColPack when g is freed
1079-
Seed = nullptr;
1080-
10811088
tape.sHInfos().Yppp_ = myalloc3(1, tape.sHInfos().p_, 1);
10821089
tape.sHInfos().Zppp_ = myalloc3(tape.sHInfos().p_, indep, 2);
10831090
tape.sHInfos().Upp_ = myalloc2(1, 2);
@@ -1162,12 +1169,18 @@ int computeSparseHess(short tag, int indep, const double *basepoint, int *nnz,
11621169
} else {
11631170
// at least one of rind cind values is not allocated, deallocate others
11641171
// and call unmanaged versions
1165-
if (*values != nullptr)
1166-
free(*values);
1167-
if (*rind != nullptr)
1168-
free(*rind);
1169-
if (*cind != nullptr)
1170-
free(*cind);
1172+
if (*values != nullptr){
1173+
delete[] *values;
1174+
*values = nullptr;
1175+
}
1176+
if (*rind != nullptr){
1177+
delete[] *rind;
1178+
*rind = nullptr;
1179+
}
1180+
if (*cind != nullptr){
1181+
delete[] *cind;
1182+
*cind = nullptr;
1183+
}
11711184
if constexpr (RCM == RecoveryMethod::Indirect)
11721185
tape.sHInfos().indirectRecover(rind, cind, values);
11731186
else if (RCM == RecoveryMethod::Direct)

ADOL-C/include/adolc/valuetape/sparseinfos.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ struct ADOLC_API SparseJacInfos {
3737
SparseJacInfos &operator=(SparseJacInfos &&other) noexcept;
3838

3939
void setJP(std::vector<uint *> &&JPIn) {
40+
// delete JP
41+
for (auto& j: JP_)
42+
delete[] j;
4043
JP_ = std::move(JPIn);
4144
depen_ = JP_.size();
4245
}
@@ -84,6 +87,9 @@ struct ADOLC_API SparseHessInfos {
8487

8588
std::vector<uint *> &getHP() { return HP_; }
8689
void setHP(int indep, std::vector<uint *> &&HPIn) {
90+
// delete HP_
91+
for (auto& h: HP_)
92+
delete[] h;
8793
indep_ = indep;
8894
HP_ = std::move(HPIn);
8995
}
@@ -98,4 +104,4 @@ struct ADOLC_API SparseHessInfos {
98104
double **values);
99105
};
100106
} // namespace ADOLC::Sparse
101-
#endif // ADOLC_SPARSE_INFOS_H
107+
#endif // ADOLC_SPARSE_INFOS_H

ADOL-C/include/adolc/valuetape/valuetape.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,4 +760,4 @@ class ADOLC_API ValueTape {
760760
CpInfos *get_cp_fct(int index) { return cp_buffer_.getElement(index); }
761761
};
762762

763-
#endif // ADOLC_VALUETAPE_H
763+
#endif // ADOLC_VALUETAPE_H

ADOL-C/src/uni5_for.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5941,7 +5941,7 @@ int hov_forward(
59415941
#if defined(_INDO_)
59425942
#if defined(_INDOPRO_)
59435943
for (int i = 0; i < max_ind_dom; i++) {
5944-
delete ind_dom[i];
5944+
delete[] ind_dom[i];
59455945
}
59465946
delete[] ind_dom;
59475947
#endif
@@ -5955,15 +5955,15 @@ int hov_forward(
59555955
for (int ii = 1; ii <= sod[i].entry; ii++) {
59565956
crs[i][ii] = temp->entry;
59575957
temp1 = temp->left;
5958-
free(temp);
5958+
delete temp;
59595959
temp = temp1;
59605960
}
59615961
}
59625962

5963-
delete sod;
5964-
delete nonl_dom;
5965-
delete fod;
5966-
delete arg_index;
5963+
delete[] sod;
5964+
delete[] nonl_dom;
5965+
delete[] fod;
5966+
delete[] arg_index;
59675967

59685968
#endif
59695969
#if defined(_NONLIND_OLD_)
@@ -5973,7 +5973,7 @@ int hov_forward(
59735973
crs[i][0] = nonl_dom[i][0];
59745974
for (l = 1; l < crs[i][0] + 1; l++)
59755975
crs[i][l] = nonl_dom[i][l + 1];
5976-
delete nonl_dom[i];
5976+
delete[] nonl_dom[i];
59775977
}
59785978
delete[] nonl_dom;
59795979

@@ -6111,7 +6111,7 @@ void merge_2_index_domains(int res, int arg, locint **ind_dom) {
61116111
k++;
61126112
}
61136113
temp_array[0] = k - 2;
6114-
delete ind_dom[res];
6114+
delete[] ind_dom[res];
61156115
ind_dom[res] = temp_array;
61166116
}
61176117
}
@@ -6146,7 +6146,7 @@ void free_tree(IndexElement *tree, int num) {
61466146
}
61476147
{
61486148
if (tree->entry == num)
6149-
free(tree);
6149+
delete tree;
61506150
}
61516151
}
61526152
void traverse_crs(IndexElement *tree, IndexElement_sod *sod, int num) {
@@ -6291,7 +6291,7 @@ void extend_nonlinearity_domain_binary_step(int arg1, int arg2,
62916291
k++;
62926292
}
62936293
temp_nonl[0] = k - 2;
6294-
delete nonl_dom[index];
6294+
delete[] nonl_dom[index];
62956295
nonl_dom[index] = temp_nonl;
62966296
}
62976297
}

0 commit comments

Comments
 (0)