Skip to content

Fix memory leaks in dudect #287

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 29, 2025

Conversation

HeatCrab
Copy link
Contributor

This commit resolves memory leaks in the original implementation and improves stability with a warm-up phase. Previously, the initialization function was called multiple times within the testing function, reallocating memory for the context array without releasing earlier allocations, resulting in leaks detected by Valgrind (43,632 bytes across 909 blocks per test). Now, the initialization happens only once, and memory is released when testing completes, preventing any leaks.

Additionally, a warm-up step is added in the measurement function by discarding the first batch of data. This filters out initial anomalies (such as cache misses or memory allocation delays), aligning with the design principles of the dudect framework and enhancing measurement consistency. Previous Chinese comments are also translated to English for better readability.

These changes ensure reliability, eliminate memory issues, and adhere to the lightweight and stable testing approach outlined in the dudect paper.

Change-Id: I34b257c2ae545c7c28c0c0be60e43988efc8158d

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refine the git commit messages. In particular, the statement "Previous Chinese comments are also translated to English for better readability" should not appear since there were no Chinese characters appearing in the git log.

Also, don't try to summarize as following:

These changes ensure reliability, eliminate memory issues, and adhere to the lightweight and stable testing approach outlined in the dudect paper.

Write down your thoughts as a human being.

@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch from 1c6d2df to 8677947 Compare March 29, 2025 16:45
@HeatCrab
Copy link
Contributor Author

HeatCrab commented Mar 29, 2025

Received. I’ve made the adjustments. The comment about Chinese was due to using Chinese in my own branch, and I’ll be mindful of this in the future. I’ve also removed the overly stiff and unnecessary summary, adding my own thoughts and reasons for the code changes instead. Please review again.

new commit 8677947

Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid bullets in git commit messages. Instead, use plain and complete English sentences. You should discuss the root cause resulting in memory leaks and why you coupled the fix for memory leaks with the warm-up procedure for duduct in the proposed pull request.

@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch from 8677947 to c469179 Compare March 29, 2025 18:01
@HeatCrab
Copy link
Contributor Author

Thanks for the feedback! About why I combined the warm-up step and memory leak fix: I was testing the warm-up idea from my old version when Valgrind caught the memory leak. Fixed that first, then added and tested the warm-up code I’d written. It had issues with the old memory release approach, but worked fine after the fix. Since it didn’t cause problems and might help later, like the paper suggests with timing stuff, I kept it instead of tossing it after the effort. I’ve submitted a new commit with these changes—happy to adjust if you’d prefer them separated.

new commit c469179

@jserv
Copy link
Contributor

jserv commented Mar 29, 2025

I’ve submitted a new commit with these changes—happy to adjust if you’d prefer them separated.

Check CONTRIBUTING.md carefully:

Group Related Changes Together: Each commit should encapsulate a single, coherent change. e.g., if you are addressing two separate bugs, create two distinct commits. This approach produces focused, small commits that simplify understanding, enable quick rollbacks, and foster efficient peer reviews. By taking advantage of Git’s staging area and selective file staging, you can craft granular commits that make collaboration smoother and more transparent.

You should provide at least two pull requests to address the proposed changes. That is, this pull request should be refined to resolve memory leaks found in 'dudect' directory, and the incoming pull request will deal with the warm-up step.

@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch 2 times, most recently from 962f03d to 92fa4fb Compare March 29, 2025 18:45
@jserv jserv changed the title Fix memory leaks and add warm-up in fixture.c Fix memory leaks in dudect Mar 29, 2025
Copy link
Contributor

@jserv jserv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't mention the file name in the subject of git commit messages. Instead, refer to its module name such as 'dudect'.

@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch 3 times, most recently from 350b6fa to e1216bb Compare March 29, 2025 19:02
@HeatCrab
Copy link
Contributor Author

HeatCrab commented Mar 29, 2025

OK! Updated and adjusted the subject of git commit messages.

latest commit e1216bb

dudect/fixture.c Outdated
static bool test_const(char *text, int mode)
{
bool result = false;

init_once(); // Initialize only once
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It does not make sense to write comment "Initialize only once" as the function name init_once already implies.

Copy link
Contributor Author

@HeatCrab HeatCrab Mar 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking it over, I considered the function name already conveys the comment’s meaning, and I couldn’t come up with a more concise or clear alternative, so I decided to remove it.

latest commit 7709ce1

@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch from e1216bb to 7709ce1 Compare March 29, 2025 19:13
dudect/fixture.c Outdated
@@ -213,18 +213,21 @@ static void init_once(void)
{
init_dut();
for (size_t i = 0; i < DUDECT_TESTS; i++) {
ctxs[i] = malloc(sizeof(t_context_t));
t_init(ctxs[i]);
if (!ctxs[i]) { // Avoid repeated allocation
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use C-style comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed !

new commit 00cd93a

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
@HeatCrab HeatCrab force-pushed the fix-fixture-memory-issue branch from 7709ce1 to 00cd93a Compare March 29, 2025 19:37
@jserv jserv merged commit fe5b1f9 into sysprog21:master Mar 29, 2025
1 of 2 checks passed
@jserv
Copy link
Contributor

jserv commented Mar 29, 2025

Thank @HeatCrab for contributing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants