<div dir="ltr">Yes, thanks for your review.<div><br></div><div style>However, in program P uses different dynamic libraries A and B,</div><div style>if A's global objects init by B's global objects, It might have the same problem even on __APPLE__ right?</div>
<div style><br></div><div style>Actually, Someone told me programmer should not rely on this idiom.</div><div style>He said, if the program uses global object init depending on another component's global object init.</div>
<div style>This program should be considered wrong.</div><div style><br></div><div style>I've no idea whether I should submit this patch,</div><div style>It seems it is a tricky that only GNU libstdc++ has.</div></div>
<div class="gmail_extra"><br><br><div class="gmail_quote">2013/3/30 Howard Hinnant <span dir="ltr"><<a href="mailto:hhinnant@apple.com" target="_blank">hhinnant@apple.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On Mar 5, 2013, at 2:35 AM, WenHan Gu (谷汶翰) <<a href="mailto:wenhan.gu@gmail.com">wenhan.gu@gmail.com</a>> wrote:<br>
<br>
> Hi all,<br>
><br>
> If I use cerr/cout... before main function, it may potentially cause use-before-init before this patch.<br>
><br>
> This is a tiny patch, but important to guarantee init-before-use.<br>
> Please review it. Thanks!!<br>
><br>
><br>
> ===<br>
><br>
> Long explanation:<br>
><br>
> std::cout, cerr, ... are global objects that should be initialized before use. However, C++ does not guarantee an order of initialization between static objects in different translation units.<br>
> One basic idiom is that declares a static object that gets created in every translation unit that includes <iostream>. This object has a static constructor and destructor that initializes and destroys the global iostream objects before they could possibly be used in the file.<br>
> In GNU libstdc++, it is at<br>
> <libstdc++>/include/std/iostream: static ios_base::Init __ioinit;<br>
> but in libcxx, it is at src/iostream.cpp, i.e., it only guarantee initialized when entering main function. If we use them before main, it may cause undefined behavior since it is a use-before-init bug.<br>
><br>
> Thanks!<br>
<br>
</div></div>Sorry for the long delay in reviewing this patch.<br>
<br>
If this patch is to be committed, it will have to be #ifdef'd out on __APPLE__. On Apple platforms we have an additional guarantee: if A links against B, B's initializer will be run before A's.<br>
<br>
I.e. if you link to libc++.dylib, then cout et al. are guaranteed to be constructed before your initializers run.<br>
<br>
Use of this guarantee is a deliberate design decision. It greatly reduces the number of global constructors (-Wglobal-constructors) in a program. This in turn can increase launch time performance.<br>
<span class="HOEnZb"><font color="#888888"><br>
Howard<br>
<br>
</font></span></blockquote></div><br><br clear="all"><div><br></div>-- <br><div dir="ltr">Best Regards,<div>WenHan Gu (Nowar)</div></div>
</div>