libstdc++/1576: crash before entering main

Florian Kainz kainz@ilm.com
Fri Jan 12 19:50:00 GMT 2001


Benjamin Kosnik wrote:
>
> Here's a slightly different way of solving this, I believe, without
> the ctor hacks and just putting all the std objects into one file
> instead.  Can you check it out please?
>
> thanks,
> benjamin
>

Benjamin,

thank you very much for your reply.

I tried your version of the bug fix, without the
constructor hacks.  For my test case, it works.
However, I believe that this is just a coincidence.
I think that for correctness, the constructor hacks
(or something similar) are necessary.  See below
for an explanation why.

Florian


------------------------------------------------------------------------------
Florian Kainz
Industrial Light + Magic
San Rafael, California
------------------------------------------------------------------------------


If I understand your changes correctly, you are assuming that
combining the definitions for ios_base::Init::_S_ios_base_init,
for the standard streams (cin, cout, cerr, clog), and for
ios_base::Init::Init(), all into one source file, forces
initialization to take place in this order:

    1) _S_ios_base_init is set to zero.

    2) The standard streams are initialized with NULL
       filebufs; the streams are not yet usable, but
       otherwise, this initialization is harmless.

    3) Initialization of some __ioinit object triggers
       the first call to ios_base::Init::Init().
       _S_ios_base_init is incremented to one, and
       ios_base::Init::Init() supplies real filebufs
       for the standard streams.  The streams are now
       usable.

According to my understanding of section 3.6.2 of the C++ standard
(in particular the example in paragraph 3), there is no guarantee
that initialization of the variables cin, cout, etc. in ios.cc
happens before any of the __ioinit objects elsewhere in the program
are initialized.  The following order of events is possible:

    1) _S_ios_base_init is set to zero.

    2) Initialization of some __ioinit object triggers
       the first call to ios_base::Init::Init().
       _S_ios_base_init is incremented to one, and
       ios_base::Init::Init() supplies filebufs for
       the standard streams.  The streams are now
       usable.

    3) cin, cout, etc. are initialized with NULL
       filebufs; the standard streams are no longer
       usable.




More information about the Gcc-bugs mailing list