Initialize function-local static variables using "T& t = *new T"#3157
Open
lixingcong wants to merge 2 commits intogabime:v1.xfrom
Open
Initialize function-local static variables using "T& t = *new T"#3157lixingcong wants to merge 2 commits intogabime:v1.xfrom
lixingcong wants to merge 2 commits intogabime:v1.xfrom
Conversation
Owner
|
Thanks but now tests fails with “LeakSanitizer: detected memory leaks”. A possible solution would be to delete those objects in spdlog::shutdown() and call it at the end of tests. |
Author
|
So hard to free memory of #include <iostream>
#include <thread>
struct A {
A(int i)
: a(i)
{
std::cout << "ctor(), i=" << a << std::endl;
}
~A()
{
std::cout << "dtor(), i=" << a << std::endl;
}
const int a;
};
static A& get_a()
{
static thread_local A& a = *new A(8);
static thread_local A b(100);
return a;
}
int main()
{
std::thread t([]() { get_a(); });
t.join();
return 0;
}Compile with gcc 11 it got weird print: That is why LeakSanitizer tell us memory leak happened. |
Owner
|
This has to solved somehow |
|
I tested this PR. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It would be safer if init the static object with reference according to Google C++ Style Guide.
I compile the static lib, link to my program, then get segmentation fault on calling
mdc::get_context(). After applying these patches the bug was gone. 💯