fix memory leak: free g_tid in Driver::runTest#32
fix memory leak: free g_tid in Driver::runTest#32sahitya-chandra wants to merge 2 commits intoosdldbt:mainfrom
Conversation
| // 0 represents the Data-Maintenance thread | ||
| for (int i = 0; i <= iUsers; i++) { | ||
| if (pthread_join(g_tid[i], NULL) != 0) { | ||
| free(g_tid); |
There was a problem hiding this comment.
This is destroying all the handles to all the threads if there is an issues joining one of them right?
There was a problem hiding this comment.
Yes sir, free(g_tid) frees the whole array, so we lose every pthread_t in one go, including threads we haven’t joined yet.
That’s intentional on this path: pthread_join failing is treated as fatal and we throw immediately afterward, so the normal driver run doesn’t continue. We still free(g_tid) so we don’t leak if the exception is ever caught or for static analysis.
I’ve added a short comment above that free in the error branch documenting this tradeoff.
There was a problem hiding this comment.
This feels a little heavy handed if threads are still running, or rather maybe what we should really do based should be based on the situation. Needs some more thought...
There was a problem hiding this comment.
I think what went wrong with my current approach is that if we free(g_tid) as soon as one pthread_join fails, we throw away every handle in one shot. Other threads can still be running or at least still need a join, so that’s heavy-handed (as pointed out by you also): we’ve lost the ability to join them even though we might still want to try.
Handle thread join failure by freeing thread IDs and throwing an error.
|
@sahitya-chandra Since you are vying for a Google Summer of Code mentorship, I'm going to respond in an unconventional way. What I was looking for is discussion that shows understanding of what the program is meant to be doing and what it should be doing instead. Your code pushes after each of my comments indicates you think there is a binary response to my questions. |
|
You’re right @markwkm sir, I treated your notes as “fix the code” instead of stepping back and explaining what it does, first What this code is doing: |
I think the better approach would be to attempt |
Summary
Fixes a memory leak in
src/Driver/Driver.cppwhereg_tidwas allocated withmallocbut never freed.Changes
g_tidafter all worker threads are joined at the end ofrunTest().g_tidbefore throwing whenpthread_joinfails so the allocation is not leaked on the error path.g_tid = NULLafter freeing to avoid use-after-free.#include <cstdlib>forfree().