Re: does g++ delay initializing static local variables?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, May 13, 2011 at 6:34 PM, Brendan Miller <catphive@xxxxxxxxxxxx> wrote:
> On Wed, May 11, 2011 at 2:35 AM, Jonathan Wakely <jwakely.gcc@xxxxxxxxx> wrote:
>> On 11 May 2011 01:52, Brendan Miller wrote:
>>>
>>> In some older versions of G++ there seems to be a bug in the lazy
>>> initialization code that was causing an exception to be thrown in some
>>> circumstances.
>>
>> Can you reliably reproduce those circumstances or is it something
>> you've only seen in occasional stack traces?
>>
>
> It's a multi-threading problem, so it's inherently not deterministic.
> However, I asked and apparently we've been seeing that code throw an
> exception on a regular basis.
>
> This is in the context of a larger c++ application... I've been trying
> to think of what reason could cause the lazy initialization code to
> throw an exception... The only case where it *should* throw is if
> there's recursion on initialization, but there is none in our code.
>
Global objects usually work fine, but can cause a lot of grief,
especially if you violate ODR. I've gotten to the point where I audit
their use because I don't like their intermittent surprises.

I've found that hiding the global object behind a [global] accessor
function works well. For example, instead of:

g_someObject;

I will use the following. I've never had a problem with double check
initializers, but some folks will object to them.

MyObject& GetMyObject()
{
    static volatile MyObject* someObject = NULL;

    // Perform double checked init, return a default object on failure

    return *someObject;
}

Jeff


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux