-
-
Notifications
You must be signed in to change notification settings - Fork 649
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
Memory leaks just by including doctest.h #205
Comments
strange... valgrind on Unix doesn't report anything. Version 2.0+ uses a lot more things from the STL - this could be a bug in the toolchain/stl (i've had such issues many times before where a specific version of the compiler is causing trouble). I'll try to reproduce it and investigate but I don't know when I'll get to this. |
The same leaks occur in VS2019 Preview 2. Further investigation shows that this is caused by g_infoContexts being thread_local. When DOCTEST_THREAD_LOCAL is defined to nothing then the leaks go away. Is it necessary to have it be thread_local if all the tests run from the same thread? |
All tests always run from the same thread but if one test case spawns multiple threads and uses asserts and logging with INFO() - then that needs to be thread-local. If you don't have such a scenario (a test case spawning multiple threads which use doctest asserts) you may indeed use DOCTEST_THREAD_LOCAL and define it to nothing. Thanks for diagnosing this - I'm very curious as to why a thread-local global std::vector is a problem. If you have any ideas I would be grateful - otherwise I'll look into it when I have the chance... But for now version 2.3 with xml reporting is the priority. |
Solution: creating the static thread_local object from a getter function solves it. Like this: std::vector<IContextScope*>& getInfoContexts() {
static DOCTEST_THREAD_LOCAL std::vector<IContextScope*> g_infoContexts; // for logging with INFO()
return g_infoContexts;
} |
Is this solution related to the "static initialization order ‘fiasco"? This global is used only after If I don't come up with anything else as a solution I might add this ... "fix" with a fat TODO next to it for the next version. |
Perhaps. It is a bit of a bad experience to see memory leaks just by including the header, though. |
So... Could you do this fix locally for your copy of doctest for now, and wait for me to try to figure this thing out when I have the time? |
Yes. I have already fixed it here locally. |
So I just tried copy-pasting your example and am unable to reproduce this with VS 2017. Are you running the executable in some special way? I haven't investigated what How did you diagnose that the problem was the thread-local global? There are other thread-local global variables apart from it... What else do you have in If anyone else can contribute information to this issue I would be grateful. |
This is with a clean new Console Application project created via the project wizard. Don't you see the memory leaks in Visual Studio's output window? |
To be clear: The memory leaks are not printed with the console output, but rather in the Output window inside Visual Studio itself. |
Well I'm going to close this... It will remain for others to find and inspect if anyone else hits this problem, but as far as I understand there shouldn't be anything wrong with that thread local, and there are no problems with the other thread locals as well... I cannot reproduce this locally - I'll need even more information on how to reproduce it - with an even smaller example... perhaps using cl.exe on the command line with the full list of flags or something, and not calling the executable through the debugger maybe... I don't know. But the fix suggested here doesn't seem right to me. You could still define DOCTEST_THREAD_LOCAL to nothing locally. |
Description
We upgraded from doctest 1.2.6 to 2.2.3. Now Visual Studio reports memory leaks.
Steps to reproduce
The following simple example code gives 3 memory leaks when run.
Extra information
The text was updated successfully, but these errors were encountered: