title: Boost property_tree error: conversion of data to type "j" failed when getting an element in a .ini file
body: <p>I have a .ini file contains below data</p>
  <pre><code>[SYSTEM]
  num_of_vps = 1
  </code></pre>
  <p>And I have this code to read an element in .ini file. (<code>uint</code> defined as <code>typedef unsigned int uint</code>)</p>
  <pre><code>boost::property_tree::ptree pt; 
  boost::property_tree::ini_parser::read_ini(iniFilePath, pt);
  hwCount = pt.get&lt;uint&gt;(&quot;SYSTEM.num_of_vps&quot;); 
  </code></pre>
  <p>I created a from files including above code and called it in a wrapper function in <code>main.cc</code> file. Then I am getting below error</p>
  <blockquote>
  <p>terminate called after throwing an instance of 'boost_1_68_0::exception_detail::clone_impl&lt;boost_1_68_0::exception_detail::error_info_injector&lt;boost_1_68_0::property_tree::ptree_bad_data&gt; &gt;'
  what():  conversion of data to type &quot;j&quot; failed
  stack trace</p>
  </blockquote>
  <pre><code>#12 0x00002aaab613fcd5 in abort () from /lib64/libc.so.6
  #13 0x00002aaab9b29315 in __gnu_cxx::__verbose_terminate_handler () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/vterminate.cc:95
  #14 0x00002aaab9a9e8f6 in __cxxabiv1::__terminate (handler=&lt;optimized out&gt;) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:47
  #15 0x00002aaab9a9e941 in std::terminate () at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_terminate.cc:57
  #16 0x00002aaab9a9ea74 in __cxxabiv1::__cxa_throw (obj=&lt;optimized out&gt;, tinfo=0x2aaab9e1ff60 &lt;typeinfo for boost_1_68_0::exception_detail::clone_impl&lt;boost_1_68_0::exception_detail::error_info_injector&lt;boost_1_68_0::property_tree::ptree_bad_data&gt; &gt;&gt;, dest=0x2aaab99bef18 &lt;boost_1_68_0::exception_detail::clone_impl&lt;boost_1_68_0::exception_detail::error_info_injector&lt;boost_1_68_0::property_tree::ptree_bad_data&gt; &gt;::~clone_impl()&gt;) at ../../../../src/gcc-7.3.0/libstdc++-v3/libsupc++/eh_throw.cc:93
  #17 0x00002aaab99bec82 in boost_1_68_0::throw_exception&lt;boost_1_68_0::exception_detail::error_info_injector&lt;boost_1_68_0::property_tree::ptree_bad_data&gt; &gt; (e=...) at throw_exception.hpp:72
  #18 0x00002aaab99be576 in boost_1_68_0::exception_detail::throw_exception_&lt;boost_1_68_0::property_tree::ptree_bad_data&gt; (x=..., current_function=0x2aaab9b45fc0 &lt;boost_1_68_0::enable_if&lt;boost_1_68_0::property_tree::detail::is_translator&lt;boost_1_68_0::property_tree::stream_translator&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;, unsigned int&gt; &gt;, unsigned int&gt;::type boost_1_68_0::property_tree::basic_ptree&lt;std::string, std::string, std::less&lt;std::string&gt; &gt;::get_value&lt;unsigned int, boost_1_68_0::property_tree::stream_translator&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;, unsigned int&gt; &gt;(boost_1_68_0::property_tree::stream_translator&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;, unsigned int&gt;) const::__PRETTY_FUNCTION__&gt; &quot;typename boost_1_68_0::enable_if&lt;boost_1_68_0::property_tree::detail::is_translator&lt;Translator&gt;, Type&gt;::type boost_1_68_0::property_tree::basic_ptree&lt;Key, Data, KeyCompare&gt;::get_value(T&quot;..., file=0x2aaab9b45830 &quot;property_tree/detail/ptree_implementation.hpp&quot;, line=675) at throw_exception.hpp:89
  #19 0x00002aaab99be01e in boost_1_68_0::property_tree::basic_ptree&lt;std::string, std::string, std::less&lt;std::string&gt; &gt;::get_value&lt;unsigned int, boost_1_68_0::property_tree::stream_translator&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;, unsigned int&gt; &gt; (this=0xbc86c8, tr=...) at property_tree/detail/ptree_implementation.hpp:673
  #20 0x00002aaab99bd6a5 in boost_1_68_0::property_tree::basic_ptree&lt;std::string, std::string, std::less&lt;std::string&gt; &gt;::get_value&lt;unsigned int&gt; (this=0xbc86c8) at property_tree/detail/ptree_implementation.hpp:683
  #21 0x00002aaab99bc545 in boost_1_68_0::property_tree::basic_ptree&lt;std::string, std::string, std::less&lt;std::string&gt; &gt;::get&lt;unsigned int&gt; (this=0x7fffffff9470, path=...) at property_tree/detail/ptree_implementation.hpp:754
  #22 0x00002aaab99bba83 in MyRT::DUTConfigFile::readIniFile (this=0xbc5d50, iniFilePath=...) at DUTConfigFile.cpp:231
  #23 0x00002aaab99ba8d2 in MyRT::DUTConfigFile::DUTConfigFile (this=0xbc5d50, iniFilePath=..., configFilePath=...) at DUTConfigFile.cpp:26
  #24 0x00002aaab99c0839 in setupMyConfigs () at SimXLInterface.cpp:83
  #31 0x0000000000408847 in main ()
  </code></pre>
  <p>I tried gdb and it is throwing an exception when converting string to uint using istringstream. Below are the two types of the transition</p>
  <pre><code>template&lt;class K, class D, class C&gt;                                                                                                                    
  template&lt;class Type&gt; inline                                                                                                                            
  Type basic_ptree&lt;K, D, C&gt;::get_value() const
  {
      return get_value&lt;Type&gt;(
          typename translator_between&lt;data_type, Type&gt;::type());
  }
  
  (gdb) p typeid(Type).name()
  $2 = 0x2aaab5c33c91 &lt;typeinfo name for unsigned int&gt; &quot;j&quot;
  (gdb) p typeid(data_type).name()
  could not find typeinfo symbol for 'std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;'
  </code></pre>
  <p>Below function in <code>basic_ios.h</code> throws a <code>__throw_bad_cast()</code> exception (here <code>this-&gt;_M_num_get = NULL</code>)</p>
  <pre><code>const __num_get_type&amp; __ng = __check_facet(this-&gt;_M_num_get);
  </code></pre>
  <p>Internal build automation Makefile is addding <code>-D_GLIBCXX_USE_CXX11_ABI=0</code> to object file creation by default</p>
  <pre><code>g++ -I/usr/myboost/boost/boost -Wall -std=c++14 -fopenmp -m64 -msse2 -fPIC \
    -D_GLIBCXX_USE_CXX11_ABI=0 -g -o *.o -fPIC -c *.cpp
  </code></pre>
  <p>.so building command</p>
  <pre><code>g++ -m64 -msse2 -pthread -shared -static-libstdc++ -static-libgcc  \
    -Wl,-znoexecstack -g -o runtime.so *.o -ldl -lrt -lz -fopenmp -lcrypto
  </code></pre>
  <p>If I change it to read a std::string type and later convert it to uint, it is working without an exception</p>
  <pre><code>std::string strHwCount = pt.get&lt;std::string&gt;(&quot;SYSTEM.num_of_vps&quot;);
  hwCount = static_cast&lt;uint&gt;(std::stoul(strHwCount));
  </code></pre>
  <p>I am using <code>boost-1.68</code> and <code>gcc-7.3</code>. Dose this have to do with the <code>ABI</code> macro that I'm using? Is there a way to resolve this without above workaround</p>
  <hr />
  <h2>UPDATE</h2>
  <p>First of all I am migrating project from cmake to a internal custom build automation (some enhanced version of Makefile). In cmake this worked fine. Based on sehe's answer I did some further analysis.
  In a small standalone example this error is not reproducible (using both local string and actual .ini file read).</p>
  <p>When going through gdb, In file <code>boost/property_tree/stream_translator.hpp</code>, I could find the read value from the file (changed to <code>num_of_vps = 4</code> in file) in the both <code>string</code> and <code>istringstream</code>.</p>
  <pre><code>optional&lt;Type&gt; basic_ptree&lt;K, D, C&gt;
      ::get_value_optional(Translator tr) const
  {
      return tr.get_value(data());
  }
  
  (gdb) p data()
  $1 = &quot;4&quot;
  
  boost_1_68_0::optional&lt;E&gt; get_value(const internal_type &amp;v) {
      std::basic_istringstream&lt;Ch, Traits, Alloc&gt; iss(v);
      iss.imbue(m_loc);
      E e;
      customized::extract(iss, e);
      ....
  }
  
  (gdb) p v
  $1 = &quot;4&quot;
  (gdb) p iss.str()
  $2 = &quot;4&quot;
  </code></pre>
  <p>Inside above <code>customized::extract(iss, e)</code> function there is a <code>string</code> to <code>unsigned int</code> conversion using a <code>basic_istream</code></p>
  <pre><code>static void extract(std::basic_istream&lt;Ch, Traits&gt;&amp; s, E&amp; e) {
      s &gt;&gt; e;
      if(!s.eof()) { s &gt;&gt; std::ws; }
  } 
  </code></pre>
  <p>Inside this <code>&gt;&gt;</code> operator there is a <code>_M_extract</code> function and inside that <code>__check_facet</code> function throws the exception (by cheking NULL of <code>this-&gt;_M_num_get</code> )</p>
  <pre><code>basic_istream&lt;_CharT, _Traits&gt;::
  _M_extract(_ValueT&amp; __v) {
    __try {
      const __num_get_type&amp; __ng = __check_facet(this-&gt;_M_num_get);
      ...
  
  inline const _Facet&amp;
  __check_facet(const _Facet* __f) {
    if (!__f)
      __throw_bad_cast();
    return *__f;
  }
  </code></pre>
  <p>stack trace above scenario</p>
  <pre><code>#0  std::istream::_M_extract&lt;unsigned int&gt;(unsigned int&amp;) (__f=0x0) at gcc-7.3.0/objdir/x86_64-centos-linux/libstdc++-v3/include/bits/basic_ios.h:49
  #1  std::istream::operator&gt;&gt;(unsigned int&amp;) (this=0x7fffffff9980, __n=@0x7fffffff997c: 0)
  #2  boost_1_68_0::property_tree::customize_stream::extract(std::istream&amp;, unsigned int&amp;) (s=..., e=@0x7fffffff997c: 0)
  #3  boost_1_68_0::property_tree::stream_translator&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt;, unsigned int&gt;::get_value(std::string const&amp;) (this=0x7fffffff9b58, v=&quot;4&quot;)
  </code></pre>
  <p>I could see the same behavior in the another place that we are using <code>std::stringstream </code>. Variable <code>ss</code> shows empty even with <code>&lt;&lt;</code> operator adding strings</p>
  <pre><code>std::stringstream ss;
  ss &lt;&lt; std::setw(8) &lt;&lt; std::setfill('0') &lt;&lt; std::hex &lt;&lt; firmId;
  </code></pre>
  <p>I had a concern about that the <code>ABI</code> flag causing this, but I was able to remove it and the issue is still there. I searched about this kind of <code>istream</code> issues but couldn't find anything useful.</p>