-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Mark at_thread_exit
list as CRT block
#4418
Comments
I strongly suspect, but haven't confirmed, that these allocations show up as leaks in the CRT debug heap leak tracking machinery. |
I wrote a small test program to investigate the memory leak: #include <crtdbg.h>
#include <cstdlib>
#include <thread>
void at_thread_exit_func() {
int* simulated_leak = (int*) malloc(sizeof(int) * 100);
}
void thread_func() {
at_thread_exit_func();
}
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
std::thread t1(thread_func);
std::thread t2(thread_func);
t1.join();
t2.join();
_CrtDumpMemoryLeaks();
return 0;
} The test showed a memory leak:
This confirms the leak in the at_thread_exit mechanism. I wanted to know if I have a proper test code. If it is ok, perhaps we can fix the leak by wrapping the allocations inside @StephanTLavavej @CaseyCarter @AlexGuteniev feedback please. |
No, it is not. You need to use |
Hi @AlexGuteniev, Thank you for your feedback. Based on your suggestion, I have adjusted the approach and used the actual Modified
|
No, you should test the original unaltered |
Hey @AlexGuteniev, thanks for responding. I’ll continue working on this. I wanted to clarify a few things to ensure I’m approaching the issue correctly: To investigate the potential memory leak, I initially altered the internal code in Is there a specific technique to expose or simulate this mechanism in an external test scenario while keeping the original code unaltered? Regarding your mention of
I appreciate your patience and guidance throughout this process; it means a lot. This is a bit new for me, so your pointers are helping a great deal |
We are looking into It is called from That one is called via
The test program should correctly test these scenarios (all three of them), and report memory leaks. If the program does not have its own leaks, but the leak from at least one of these functions is seen, then this leak exists, and a PR needed to fix and test the fix. |
Thanks @AlexGuteniev Alex, I will work on this and get back, once again, thank you for all the help! |
Hi @AlexGuteniev, hope you are well, thank you for your patience on this one I tested the scenarios involving #include <condition_variable>
#include <crtdbg.h>
#include <cstdlib>
#include <future>
#include <iostream>
#include <mutex>
#include <thread>
void test_notify_all_at_thread_exit() {
std::mutex mtx;
std::unique_lock<std::mutex> lock(mtx);
std::condition_variable cv;
std::thread([&] {
std::notify_all_at_thread_exit(cv, std::move(lock));
}).join();
}
void test_set_value_at_thread_exit() {
std::promise<void> promise;
std::future<void> future = promise.get_future();
std::thread([&] { promise.set_value_at_thread_exit(); }).join();
future.wait();
}
void test_set_exception_at_thread_exit() {
std::promise<void> promise;
std::future<void> future = promise.get_future();
std::thread([&] {
try {
throw std::runtime_error("Test exception");
} catch (...) {
promise.set_exception_at_thread_exit(std::current_exception());
}
}).join();
try {
future.get();
} catch (const std::exception& e) {
std::cerr << "Caught exception: " << e.what() << std::endl;
}
}
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
test_notify_all_at_thread_exit();
test_set_value_at_thread_exit();
test_set_exception_at_thread_exit();
_CrtDumpMemoryLeaks();
return 0;
} When running this code, I am encountering an
The code does detect memory leaks when I continue past the However, I'm currently unable to understand the root cause of the @StephanTLavavej , @CaseyCarter it would be great if you can also provide your input as well. |
You move the lock into a thread in |
Hi @AlexGuteniev since it is an undefined behaviour and I can see you raised a PR for it, should I wait until this PR work is done? Please suggest. |
My PR only improves diagnostic for this problem, you still have to fix your code. It is also already merged. |
This isn't going to work well. You're setting up the allocations that we want to test on temporary threads. When those temporary threads complete, there will be a bit of extra processing in each case ("at thread exit", as designed) which consumes the allocation. By the time the program gets to I think you need to restructure the program so it creates all necessary objects, and then calls _CrtDumpMemoryLeaks();
std::cout << "\n\n";
std::notify_all_at_thread_exit(cv, std::move(lock));
_CrtDumpMemoryLeaks();
std::cout << "\n\n";
promise.set_value_at_thread_exit();
_CrtDumpMemoryLeaks();
std::cout << "\n\n";
// ... Comparing the differences in the output of each call to |
Hello@CaseyCarter and @AlexGuteniev , hope you guys are doing well and the new year is going good. Thanks for the inputs. I was on vacation and came back last month. I will be restarting work on this and will seek feedback as I move forward, thanks for being helpful so far. |
STL/stl/src/xnotify.cpp
Lines 41 to 45 in c53ac59
The text was updated successfully, but these errors were encountered: