Skip to content
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: Construct globals on first use #15266

Merged
merged 1 commit into from Feb 4, 2019
Merged

Conversation

@MarcoFalke
Copy link
Member

@MarcoFalke MarcoFalke commented Jan 25, 2019

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 #15111)
@sipa
Copy link
Member

@sipa sipa commented Jan 25, 2019

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().

Copy link
Member

@promag promag left a 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.

```cpp #include #include

using 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.

src/sync.cpp Outdated Show resolved Hide resolved
@MarcoFalke
Copy link
Member Author

@MarcoFalke MarcoFalke commented Jan 26, 2019

COFU<string> const g_str_new;

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.

@promag
Copy link
Member

@promag promag commented Jan 26, 2019

@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.

@sipa
Copy link
Member

@sipa sipa commented Jan 26, 2019

@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.

@MarcoFalke
Copy link
Member Author

@MarcoFalke MarcoFalke commented Jan 26, 2019

Ah, that makes it clearer.

@sipa
Copy link
Member

@sipa sipa commented Jan 26, 2019

Relevant section from https://en.cppreference.com/w/cpp/language/constant_initialization:

The effects of constant initialization are the same as the effects of the corresponding initialization, except that it's guaranteed that it is complete before any other initialization of a static or thread-local object begins, and it may be performed at compile time.

@DrahtBot
Copy link
Contributor

@DrahtBot DrahtBot commented Jan 26, 2019

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #15329 (Fix InitError() and InitWarning() content by hebasto)
  • #14169 (add -debuglogsize= option by SuckShit)
  • #13746 (-masterdatadir for datadir bootstrapping by kallewoof)
  • #13088 (Log early messages with -printtoconsole by ajtowns)

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.

@MarcoFalke MarcoFalke force-pushed the Mf1901-cofu branch 2 times, most recently from fa5e96c to fad73c1 Jan 26, 2019
src/sync.cpp Outdated Show resolved Hide resolved
src/sync.cpp Outdated Show resolved Hide resolved
src/sync.cpp Outdated Show resolved Hide resolved
src/sync.cpp Outdated Show resolved Hide resolved
@MarcoFalke MarcoFalke force-pushed the Mf1901-cofu branch 2 times, most recently from fa169ff to faacd53 Jan 27, 2019
Copy link
Member

@promag promag left a comment

ACK faacd53.

src/logging.cpp Outdated Show resolved Hide resolved
src/sync.cpp Outdated Show resolved Hide resolved
src/util/memory.h Outdated Show resolved Hide resolved
src/util/memory.h Outdated Show resolved Hide resolved
src/util/memory.h Outdated Show resolved Hide resolved
@MarcoFalke MarcoFalke force-pushed the Mf1901-cofu branch 3 times, most recently from fa978d1 to fa8e2b7 Jan 29, 2019
@MarcoFalke
Copy link
Member Author

@MarcoFalke MarcoFalke commented Jan 29, 2019

Reverted back to my initial patch (using the LogInstance wording suggested here #15266 (comment))

Copy link
Contributor

@ryanofsky ryanofsky left a comment

utACK fa8e2b7

src/logging.cpp Show resolved Hide resolved
Copy link
Contributor

@ryanofsky ryanofsky left a comment

utACK 77777c5. Just comment change since last review. Also the commit hash no longer begins with fa (in case that is a problem).

@jonasschnelli
Copy link
Contributor

@jonasschnelli jonasschnelli commented Feb 4, 2019

utACK 77777c5

MarcoFalke added a commit to MarcoFalke/bitcoin-core that referenced this issue Feb 4, 2019
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
@MarcoFalke MarcoFalke merged commit 77777c5 into bitcoin:master Feb 4, 2019
2 checks passed
@MarcoFalke MarcoFalke deleted the Mf1901-cofu branch Feb 4, 2019
laanwj added a commit that referenced this issue Feb 5, 2019
e1b6436 Fix build after pr 15266 merged (Hennadii Stepanov)

Pull request description:

  Ref: #15266

Tree-SHA512: 6647264c3e3d94f0f10dc3bed1b82dfe8ed1192906270b0bb79f4d018807e06cb42286c86ced7fee0e2b38284739657336672c5a30650b6473ffafd65c315349
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 12, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 12, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 13, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 13, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 14, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 14, 2021
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
vijaydasmp added a commit to vijaydasmp/dash that referenced this issue Sep 16, 2021
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

7 participants