C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Catch exception thrown from constructor of global object

From: Federico Kircheis <federico_at_[hidden]>
Date: Fri, 13 Jan 2023 08:40:44 +0100
On Thu, Jan 12, 2023 at 10:57:10AM -0800, Thiago Macieira via Std-Proposals wrote:
>On Thursday, 12 January 2023 07:34:28 PST Federico Kircheis via Std-Proposals
>wrote:
>> Also std::optional as global variable is problematic if inside a
>> library, depending on the visibility of the symbols, the destructor
>> might be executed more than once (and thus be UB)
>
>In other words, it should be exactly as you've intended and coded.
>
>A globally-visible symbol should register its destructor exactly once. A
>locally-visible ("hidden") symbol should register its destructor exactly once
>in each library it is used.
>

Sigh, I wish it would be that easy...

https://fekir.info/post/global-variables-in-cpp-libraries/

If the symbol is visible, and you library is statically linked in two or
more shared libraries, the constructor is executed more than once *on
the same address*.

I also gave a talk on this topic at embo++


Best

Federico

Received on 2023-01-13 07:40:56