Boost C++ Libraries: Ticket #10228: boost::filesystem::path::parent_path crashes with static link https://svn.boost.org/trac10/ticket/10228 <p> with following code </p> <pre class="wiki">#include &lt;string&gt; #include &lt;boost/filesystem.hpp&gt; #include &lt;stdint.h&gt; #include &lt;locale&gt; #include &lt;limits.h&gt; #include &lt;unistd.h&gt; #include &lt;sys/types.h&gt; using namespace std; using namespace boost::filesystem; namespace __exeinfo_private { static std::string getexepath() { char result[PATH_MAX]; ssize_t count = readlink("/proc/self/exe", result, PATH_MAX); return std::string(result, count &gt; 0 ? count : 0); } static int64_t get_current_process_id() { // from int32_t to int64_t return getpid(); } } static void run() { string _path = __exeinfo_private::getexepath(); path p(_path); if(!p.empty() &amp;&amp; p.has_filename() &amp;&amp; !p.parent_path().empty()) cout &lt;&lt; p.filename().string() &lt;&lt; endl &lt;&lt; p.parent_path().string() &lt;&lt; endl; } const static class ABC { private: ABC() { run(); } public: static const ABC&amp; instance() { static const ABC i; return i; } }&amp; instance = ABC::instance(); int main() { run(); } </pre><p> compile with g++ exeinfo.cpp -lboost_filesystem -lboost_system, everything is ok, the output is expected as &lt;the second and fourth lines are the path where the binary exists&gt; a.out /home/hzj-jie/tmp a.out /home/hzj-jie/tmp </p> <p> compile with g++ exeinfo.cpp -lboost_filesystem -lboost_system -static, static link with boost, the line 41, which is the constructor of ABC class will crash. the call stack is, </p> <pre class="wiki">#0 0x0000000000433455 in std::locale::locale(std::locale const&amp;) () #1 0x000000000040223c in boost::filesystem::path::codecvt() () #2 0x0000000000403927 in boost::filesystem::path::parent_path() const () #3 0x00000000004015d9 in run () at exeinfo.cpp:32 #4 0x0000000000401a77 in ABC::ABC (this=0x74ee60 &lt;ABC::instance()::i&gt;) at exeinfo.cpp:41 #5 0x0000000000401aaf in ABC::instance () at exeinfo.cpp:47 #6 0x00000000004017a5 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at exeinfo.cpp:50 #7 0x00000000004017c1 in _GLOBAL__sub_I_main () at exeinfo.cpp:55 #8 0x000000000045f5d7 in __libc_csu_init () #9 0x000000000045f079 in __libc_start_main () #10 0x00000000004013a7 in _start () </pre><p> according to some basic investigation, the trouble is coming from libs/filesystem/src/path.cpp, while the path_locale has not been initialized before ABC::ABC() running. because of the difference of implementation, in windows, no matter static or dynamic link, will not cause crash. please kindly let me know if this is by design, or has been fixed in the coming release, or if there were some work around. </p> <p> thank you in advanced. </p> <p> the environment i am now using is, Linux hzj-jie-x61t 3.13.0-32-generic <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/57" title="#57: Tasks: Virtual Target Representation (closed: Fixed)">#57</a>-Ubuntu SMP Tue Jul 15 03:51:08 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1) but i have tried this on an x86 machine, which has the same experience. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10228 Trac 1.4.3 anonymous Wed, 23 Jul 2014 05:13:50 GMT component changed; owner set https://svn.boost.org/trac10/ticket/10228#comment:1 https://svn.boost.org/trac10/ticket/10228#comment:1 <ul> <li><strong>owner</strong> set to <span class="trac-author">Beman Dawes</span> </li> <li><strong>component</strong> <span class="trac-field-old">None</span> → <span class="trac-field-new">filesystem</span> </li> </ul> Ticket Beman Dawes Fri, 25 Jul 2014 00:09:19 GMT status changed https://svn.boost.org/trac10/ticket/10228#comment:2 https://svn.boost.org/trac10/ticket/10228#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> <p> I have not been able to reproduce the problem. Your test program runs fine for me for both static and shared linking. I'm testing on Ubuntu 14.4 LTS with gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2. Using b2 (i.e. bjam) to do the build. </p> <p> How are you doing the build? What does your command line look like? </p> <p> Thanks, </p> <p> --Beman </p> <p> </p> Ticket anonymous Fri, 25 Jul 2014 09:15:48 GMT <link>https://svn.boost.org/trac10/ticket/10228#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:3</guid> <description> <pre class="wiki">to install boost, 1. wget ... 2. gzip ... 3. tar ... 4. ./bootstrap.sh 5. ./b2 ; ./b2 install to compile the program shared link, g++ exeinfo.cpp -lboost_filesystem -lboost_system static link, g++ exeinfo.cpp -lboost_filesystem -lboost_system -static </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Beman Dawes</dc:creator> <pubDate>Sun, 27 Jul 2014 14:53:31 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/10228#comment:4 https://svn.boost.org/trac10/ticket/10228#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">worksforme</span> </li> </ul> <p> I still can't reproduce the problem here, even doing the Boost install and test program build on a virtual machine exactly as you did. </p> <p> Is it possible your locale is somehow messed up? For example, is your LANG environmental variable set to a valid locale? </p> <p> Have you tried on a freshly built virtual machine? If the test program works statically in that environment, perhaps you can figure out what is different about the environment where the program is failing. </p> <p> I'm closing the issue as "worksforme" since after quite a bit of effort I still can't reproduce the problem. </p> <p> --Beman </p> Ticket Hzj_jie <hzj_jie@…> Sun, 27 Jul 2014 15:28:21 GMT <link>https://svn.boost.org/trac10/ticket/10228#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:5</guid> <description> <p> my LANG environment variable has been set to LANG="en_US.UTF-8", which is default by my ubuntu installation. i am now trying with other distributions, but would you mind to tell me your environment variables, so i can figure out if anything different. following is the environment variables may be related. </p> <pre class="wiki">declare -x LANG="en_US.UTF-8" declare -x LANGUAGE="en_US" declare -x LC_ADDRESS="zh_CN.UTF-8" declare -x LC_IDENTIFICATION="zh_CN.UTF-8" declare -x LC_MEASUREMENT="zh_CN.UTF-8" declare -x LC_MONETARY="zh_CN.UTF-8" declare -x LC_NAME="zh_CN.UTF-8" declare -x LC_NUMERIC="zh_CN.UTF-8" declare -x LC_PAPER="zh_CN.UTF-8" declare -x LC_TELEPHONE="zh_CN.UTF-8" declare -x LC_TIME="zh_CN.UTF-8" </pre> </description> <category>Ticket</category> </item> <item> <author>Hzj_jie <hzj_jie@…></author> <pubDate>Sun, 27 Jul 2014 19:11:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10228#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:6</guid> <description> <p> detail frame is </p> <pre class="wiki">#0 0x0000000000431555 in std::locale::locale(std::locale const&amp;) () #1 0x0000000000403ce4 in boost::filesystem::path::imbue (loc=...) at libs/filesystem/src/path.cpp:918 #2 0x0000000000403c57 in boost::filesystem::path::codecvt () at libs/filesystem/src/path.cpp:911 #3 0x0000000000404200 in boost::filesystem::path::path&lt;char const*&gt; (this=0x7fffffffe3e0, begin=0x767bd8 "/root/a.out", end=0x767bdd "/a.out") at ./boost/filesystem/path.hpp:167 #4 0x000000000040249f in boost::filesystem::path::parent_path (this=0x7fffffffe3d0) at libs/filesystem/src/path.cpp:352 #5 0x0000000000401509 in run () at exeinfo.cpp:32 #6 0x00000000004019a7 in ABC::ABC (this=0x74de60 &lt;ABC::instance()::i&gt;) at exeinfo.cpp:41 #7 0x00000000004019df in ABC::instance () at exeinfo.cpp:47 #8 0x00000000004016d5 in __static_initialization_and_destruction_0 (__initialize_p=1, __priority=65535) at exeinfo.cpp:50 #9 0x00000000004016f1 in _GLOBAL__sub_I_main () at exeinfo.cpp:55 #10 0x000000000045d5e7 in __libc_csu_init () #11 0x000000000045d089 in __libc_start_main () #12 0x00000000004012d7 in _start () </pre><p> according to std lib implementation, the copy constructor of locale will copy the _M_impl pointer and add reference to it. </p> <pre class="wiki">00058 locale::locale(const locale&amp; __other) throw() 00059 : _M_impl(__other._M_impl) 00060 { _M_impl-&gt;_M_add_reference(); } </pre><p> so if the path_locale._M_impl is nullptr, i.e. the constructor of path_locale has not been called, the copy constructor will crash. when the user trying to use static variable in functions, the logic may call the path::imbue before the constructor of path_locale, and cause the trouble. </p> <p> i have tried to provide a fix, the principle is to define path_locale as a static variable in a function just as what windows implementation does. which can resolve the bug. the diff is, </p> <pre class="wiki">root@hzj-ubuntu-1c:~# diff downloads/boost_1_55_0/libs/filesystem/src/path.cpp path.cpp 868,872c868 &lt; inline std::locale path_locale() // initialized by path::codecvt() below &lt; { &lt; static std::locale loc; &lt; return loc; &lt; } --- &gt; std::locale path_locale; // initialized by path::codecvt() below 922,923c918,919 &lt; std::locale temp(path_locale()); &lt; path_locale() = loc; --- &gt; std::locale temp(path_locale); &gt; path_locale = loc; 925c921 &lt; &amp;std::use_facet&lt;std::codecvt&lt;wchar_t, char, std::mbstate_t&gt; &gt;(path_locale()); --- &gt; &amp;std::use_facet&lt;std::codecvt&lt;wchar_t, char, std::mbstate_t&gt; &gt;(path_locale); </pre><p> would you please have a look, if the proposal works? </p> <p> thank you. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Beman Dawes</dc:creator> <pubDate>Tue, 29 Jul 2014 12:16:49 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10228#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:7</guid> <description> <p> I'm having trouble applying your patch. </p> <p> Please send me the complete modified path.cpp file. </p> <p> Thanks, </p> <p> --Beman </p> </description> <category>Ticket</category> </item> <item> <author>Hzj_jie <hzj_jie@…></author> <pubDate>Tue, 29 Jul 2014 12:58:46 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/10228 https://svn.boost.org/trac10/ticket/10228 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">path.cpp</span> </li> </ul> Ticket Hzj_jie <hzj_jie@…> Tue, 29 Jul 2014 13:02:26 GMT <link>https://svn.boost.org/trac10/ticket/10228#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:8</guid> <description> <p> attached, thank you. </p> </description> <category>Ticket</category> </item> <item> <author>Hzj_jie <hzj_jie@…></author> <pubDate>Wed, 30 Jul 2014 08:33:30 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10228#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10228#comment:9</guid> <description> <p> i saw the issue has been fixed in 1.56, thank you for your time. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Beman Dawes</dc:creator> <pubDate>Thu, 07 Aug 2014 13:00:00 GMT</pubDate> <title>milestone changed https://svn.boost.org/trac10/ticket/10228#comment:10 https://svn.boost.org/trac10/ticket/10228#comment:10 <ul> <li><strong>milestone</strong> <span class="trac-field-old">To Be Determined</span> → <span class="trac-field-new">Boost 1.56.0</span> </li> </ul> Ticket