<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:"Book Antiqua";
panose-1:2 4 6 2 5 3 5 3 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Book Antiqua",serif;
color:windowtext;
font-weight:normal;
font-style:normal;
text-decoration:none none;}
.MsoChpDefault
{mso-style-type:export-only;
font-family:"Calibri",sans-serif;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-IE link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>I have a test that has been failing for a long time, and finally got around to investigating it. The test is really simple:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>#include <iostream><o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>#include <memory><o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>struct Test {<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> int n;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> Test(int x) : n(x) { std::cerr << "Creating " << n << std::endl; }<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> ~Test() { std::cerr << "Deleting " << n << std::endl; }<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>};<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>int main () {<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> std::shared_ptr<Test> x(std::make_shared<Test>(42));<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> x.reset();<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> std::cerr << "Completed</span><span style='font-size:10.0pt'> </span><span style='font-size:10.0pt;font-family:"Courier New"'>" << std::endl;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>}<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>but it crashes immediately after printing the message from the destructor, and before the message in ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>main</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>After investigating, I found that the test works perfectly with RTTI enabled, but crashes if it is disabled which puzzled me. My LibC++ library is built with RTTI enabled as instructed on ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>libcxx.llvm.org</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ which says that the library must be built with RTTI enabled, though it may be used with RTTI disabled.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>What I found was that the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>vtable</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ for the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>shared_ptr</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ specialisation is different depending on whether RTTI is enabled or disabled. With RTTI disabled it is:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>_ZTVNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEEE:<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word 0<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word 0<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEED1Ev<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEED0Ev<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEE16__on_zero_sharedEv<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEE21__on_zero_shared_weakEv<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>but with RTTI enabled it is:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'>_ZTVNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEEE:<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word 0<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZTINSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEEE<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEED1Ev<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEED0Ev<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEE16__on_zero_sharedEv<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><b><span style='font-size:10.0pt;font-family:"Courier New"'> <span style='background:yellow;mso-highlight:yellow'>.word _ZNKSt3__119__shared_weak_count13__get_deleterERKSt9type_info</span><o:p></o:p></span></b></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New"'> .word _ZNSt3__120__shared_ptr_emplaceI4TestNS_9allocatorIS1_EEE21__on_zero_shared_weakEv<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>In the library, the implementation (in ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>memory.cpp</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ compiler with ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>-frtti</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’) is attempting to call the function ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>__on_zero_shared_weak</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’, but using offset 24, and the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>vtable</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ emitted in the test case (compiled with ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>-fno-rtti</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’) has this function at offset 20.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>This is caused by the following lines in ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'><memory></span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’:<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'> // Define the function out only if we build static libc++ without RTTI.<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'> // Otherwise we may break clients who need to compile their projects with<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'> // -fno-rtti and yet link against a libc++.dylib compiled<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'> // without -fno-rtti.<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'>#if !defined(_LIBCPP_NO_RTTI) || !defined(_LIBCPP_BUILD_STATIC)<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'> virtual const void* __get_deleter(const type_info&) const _NOEXCEPT;<o:p></o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-size:12.0pt;font-family:"Courier New"'>#endif<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>and because the function is virtual, the layout of the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>vtable</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ is different between RTTI enabled and disabled (we are building a static library, so ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>_LIBCPP_BUILD_STATIC</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ is true).<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>There are a couple of places in ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'><memory></span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ and ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>memory.cpp</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ where this happens (always with ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>__get_deleter</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’), but after sanity checking the other headers, I see that ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'><functional></span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ and ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'><__functional_03></span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ also have similar issues where the layout of the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>vtable</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ is different depending on whether RTTI is enabled or not; though I don’t have any tests which show this.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>I don’t know what the best fix is for this because it was clearly introduced to address some issue with dynamic libraries, but locally I have decided to always define ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>__get_deleter</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’, and make its implementation return ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>nullptr</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ when RTTI is disabled. This preserves the ‘</span><span style='font-size:12.0pt;font-family:"Courier New"'>vtable</span><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>’ layout independent of RTTI.<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'>Thanks,<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'> MartinO<o:p></o:p></span></p><p class=MsoNormal><span style='font-size:12.0pt;font-family:"Book Antiqua",serif'><o:p> </o:p></span></p></div></body></html>