Skip to content

Conversation

@maflcko
Copy link
Member

@maflcko maflcko 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:

@sipa
Copy link
Member

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
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@maflcko
Copy link
Member Author

maflcko 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
Contributor

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

@maflcko
Copy link
Member Author

maflcko commented Jan 26, 2019

Ah, that makes it clearer.

@sipa
Copy link
Member

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

@maflcko maflcko force-pushed the Mf1901-cofu branch 2 times, most recently from fa5e96c to fad73c1 Compare January 26, 2019 02:07
@maflcko maflcko force-pushed the Mf1901-cofu branch 2 times, most recently from fa169ff to faacd53 Compare January 27, 2019 03:37
Copy link
Contributor

@promag promag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK faacd53.

@maflcko maflcko force-pushed the Mf1901-cofu branch 3 times, most recently from fa978d1 to fa8e2b7 Compare January 29, 2019 19:15
@maflcko
Copy link
Member Author

maflcko 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

utACK fa8e2b75ff7de6b09f97512256092c1d03edef01

Copy link
Contributor

@ryanofsky ryanofsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

utACK 77777c5

maflcko pushed a commit to maflcko/bitcoin-core that referenced this pull request 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
@maflcko maflcko merged commit 77777c5 into bitcoin:master Feb 4, 2019
@maflcko maflcko deleted the Mf1901-cofu branch February 4, 2019 19:30
laanwj added a commit that referenced this pull request Feb 5, 2019
e1b6436 Fix build after pr 15266 merged (Hennadii Stepanov)

Pull request description:

  Ref: #15266

Tree-SHA512: 6647264c3e3d94f0f10dc3bed1b82dfe8ed1192906270b0bb79f4d018807e06cb42286c86ced7fee0e2b38284739657336672c5a30650b6473ffafd65c315349
vijaydasmp pushed a commit to vijaydasmp/dash that referenced this pull request 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 pushed a commit to vijaydasmp/dash that referenced this pull request 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
@bitcoin bitcoin locked as resolved and limited conversation to collaborators Dec 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants