This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: c++ static initialization question


2010/3/5 Young, Michael <Michael.Young@paetec.com>:
>> The problem
>> -----------
>> In short I have a class "Logger". In its constructor are initialized some static pointers.
>> ?1) first a static Logger object start constructing.
>> ?2) In the midle of the constructor, another static Logger object from another
>> ? ? translation unit start its constructor
>> ?3) It enters for second time in the initialization of the same static pointers before the
>> ? ? first initialization finished.
>>
>> My question is. Where can I search for information about static initialization in order to avoid this? How can I modify my code?
>
> This really isn't a gcc compiler issue - it's a general "how-to-do-X in C++" question; you'd likely be better served via a different mailing list / forum.
>
> Having said that, here's some direction and things to look at -
>
> If I understand correctly, the ctor of one Logger explicity calls the ctor of another Logger ...

No, that's not the case. Logger ctor does not create another Logger
object. What happens is: in static initialization of the first static
Logger object, gcc inserts code that calls static initialization for
another translation unit that have another static Logger object.
This happens only in one platform with certain compiler/linker flags.

I do not think it is a bug on gcc. I just want to know if there are
some docs that describes how static initializations is in gcc.

It wouldn't be difficult to code things to make Logger ctor reentrant
(http://en.wikipedia.org/wiki/Reentrant_%28subroutine%29) with respect
to this static pointers initialization. But I want to know the rules
in order to check the whole library for another similar cases.

Thanks for your response Michael.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]