memory: Construct globals on first use#15266
Conversation
|
Just as a way to minimize the diff (which may or may not be desirable): you can have a trivial proxy object instantiated as g_logger whose operator->() invokes GetLogger(). |
There was a problem hiding this comment.
An alternative is to use templating and also defer instantiation to operator->(). This way the diff would be smaller and the code less verbose.
Details
```cpp #include #includeusing namespace std;
template
class COFU
{
T* get() {
static T v;
return &v;
}
public:
T* operator->() {
return get();
}
T* const operator->() const {
return const_cast<COFU>(this)->get();
}
T& operator() {
return get();
}
T& operator() const {
return const_cast<COFU>(this)->get();
}
};
string* const g_str_old = new string();
COFU const g_str_new;
int main()
{
g_str_old->append("Hello!");
g_str_new->append("Hello!");
cout << *g_str_old << endl;
cout << *g_str_new << endl;
return 0;
}
</details>
Edit: oh @sipa already suggested above.
It looks like you are introducing a new global for this, which again opens up the problem I am trying to solve. Either I am missing something or we are running in circles. |
|
@MarcoFalke my suggestion is to do: COFU<BCLog::Logger> const g_logger;so that the following is unchanged: g_logger->m_reopen_file = true;Like @sipa suggested. |
|
@MarcoFalke Not sure if that's what you're referring to, but the COFU object has no member variables to initialize, so it doesn't have any runtime initialization (in other words, it's fully constructed before any code is executed). I don't have a strong opinion on whether such a COFU object/type is the right solution; just offering it as a possibility if reducing the size of the diff is wanted. |
|
Ah, that makes it clearer. |
|
Relevant section from https://en.cppreference.com/w/cpp/language/constant_initialization:
|
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ConflictsReviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. |
fa5e96c to
fad73c1
Compare
fa169ff to
faacd53
Compare
fa978d1 to
fa8e2b7
Compare
|
Reverted back to my initial patch (using the |
ryanofsky
left a comment
There was a problem hiding this comment.
utACK fa8e2b75ff7de6b09f97512256092c1d03edef01
|
utACK 77777c5 |
77777c5 log: Construct global logger on first use (MarcoFalke) Pull request description: The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed. Specifically this fixes: * `g_logger` might not be initialized on the first use, so do that. (Fixes bitcoin#15111) Tree-SHA512: eb9c22f4baf31ebc5b0b9ee6a51d1354bae1f0df186cc0ce818b4483c7b5a7f90268d2b549ee96b4c57f8ef36ab239dc6497f74f3e2ef166038f7437c368297d
77777c5 log: Construct global logger on first use (MarcoFalke) Pull request description: The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed. Specifically this fixes: * `g_logger` might not be initialized on the first use, so do that. (Fixes bitcoin#15111) Tree-SHA512: eb9c22f4baf31ebc5b0b9ee6a51d1354bae1f0df186cc0ce818b4483c7b5a7f90268d2b549ee96b4c57f8ef36ab239dc6497f74f3e2ef166038f7437c368297d
77777c5 log: Construct global logger on first use (MarcoFalke) Pull request description: The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed. Specifically this fixes: * `g_logger` might not be initialized on the first use, so do that. (Fixes bitcoin#15111) Tree-SHA512: eb9c22f4baf31ebc5b0b9ee6a51d1354bae1f0df186cc0ce818b4483c7b5a7f90268d2b549ee96b4c57f8ef36ab239dc6497f74f3e2ef166038f7437c368297d
The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed.
Specifically this fixes:
g_loggermight not be initialized on the first use, so do that. (Fixes Avoid UB (member call on nullptr) when failing to read randomness in the startup process #15111)