Skip to content

Commit 7709ce1

Browse files
committed
Improve dudect test with memory leak fix
Valgrind inspection revealed memory leaks in the original code, with 43,632 bytes lost across 909 blocks per test. Analysis showed that the initialization function was being called repeatedly within the testing loop, each time reallocating the context array without any provision to free prior memory allocations. This uncontrolled repetition caused memory to accumulate not freed over multiple test iterations, leading to the observed leaks. To correct this, the initialization process was revised to execute only once before testing begins, and a cleanup was added to release all allocated memory when testing concludes. To further prevent memory-related issues, the measurement function was updated to check all memory allocations, ensuring robustness against potential failures. Change-Id: If7001c1a719c6c353a37d10874c2a60e56f935d4
1 parent f53314e commit 7709ce1

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

dudect/fixture.c

+8-4
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ static bool doit(int mode)
187187
int64_t *percentiles = calloc(NUM_PERCENTILES, sizeof(int64_t));
188188

189189
if (!before_ticks || !after_ticks || !exec_times || !classes ||
190-
!input_data) {
190+
!input_data || !percentiles) {
191191
die();
192192
}
193193

@@ -213,18 +213,21 @@ static void init_once(void)
213213
{
214214
init_dut();
215215
for (size_t i = 0; i < DUDECT_TESTS; i++) {
216-
ctxs[i] = malloc(sizeof(t_context_t));
217-
t_init(ctxs[i]);
216+
if (!ctxs[i]) { // Avoid repeated allocation
217+
ctxs[i] = malloc(sizeof(t_context_t));
218+
t_init(ctxs[i]);
219+
}
218220
}
219221
}
220222

221223
static bool test_const(char *text, int mode)
222224
{
223225
bool result = false;
224226

227+
init_once();
228+
225229
for (int cnt = 0; cnt < TEST_TRIES; ++cnt) {
226230
printf("Testing %s...(%d/%d)\n\n", text, cnt, TEST_TRIES);
227-
init_once();
228231
for (int i = 0; i < ENOUGH_MEASURE / (N_MEASURES - DROP_SIZE * 2) + 1;
229232
++i)
230233
result = doit(mode);
@@ -235,6 +238,7 @@ static bool test_const(char *text, int mode)
235238

236239
for (size_t i = 0; i < DUDECT_TESTS; i++) {
237240
free(ctxs[i]);
241+
ctxs[i] = NULL;
238242
}
239243

240244
return result;

0 commit comments

Comments
 (0)