Skip to content

Commit 00cd93a

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: Id5e7b24447cb5b7797ef19c4bfdb79060fb299fa
1 parent f53314e commit 00cd93a

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

dudect/fixture.c

+11-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,24 @@ 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+
/* Check if ctxs[i] is unallocated to prevent repeated memory
217+
* allocations.
218+
*/
219+
if (!ctxs[i]) {
220+
ctxs[i] = malloc(sizeof(t_context_t));
221+
t_init(ctxs[i]);
222+
}
218223
}
219224
}
220225

221226
static bool test_const(char *text, int mode)
222227
{
223228
bool result = false;
224229

230+
init_once();
231+
225232
for (int cnt = 0; cnt < TEST_TRIES; ++cnt) {
226233
printf("Testing %s...(%d/%d)\n\n", text, cnt, TEST_TRIES);
227-
init_once();
228234
for (int i = 0; i < ENOUGH_MEASURE / (N_MEASURES - DROP_SIZE * 2) + 1;
229235
++i)
230236
result = doit(mode);
@@ -235,6 +241,7 @@ static bool test_const(char *text, int mode)
235241

236242
for (size_t i = 0; i < DUDECT_TESTS; i++) {
237243
free(ctxs[i]);
244+
ctxs[i] = NULL;
238245
}
239246

240247
return result;

0 commit comments

Comments
 (0)