<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class=""><div class="">Hi folks,</div><div class=""><br class=""></div><div class="">I’d like to add a flag in clang, <font face="Courier New" class="">-fno-cxx-static-destructors</font>, which allows developers to demand that no static destructors be emitted. Bruno has <a href="https://reviews.llvm.org/D22474" class="">a sample implementation</a>. We’ve <a href="http://lists.llvm.org/pipermail/cfe-dev/2016-July/050040.html" class="">discussed this previously</a> but I’d like to re-open the discussion and make a different case for it because we’ve received more requests for such a feature.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Why is this desirable?</b></div><div class=""><br class=""></div><div class="">In low-memory circumstances it’s often the case that we know that static global destructors are never called. It would be useful to avoid emitting them entirely to save memory. We can’t necessarily make the types themselves trivially destructible (e.g. a <font face="Courier New" class="">std::map</font>, or a type that’s used both as a global and as an automatic variable for which the destructor is only meaningful when automatic, or coming from 3rd party library such as boost), and using a <font face="Courier New" class="">NeverDestroyed<T></font> class or global pointer only (<font face="Courier New" class="">std::string& foo = *new std::string(“derp");</font>) prevents <font face="Courier New" class="">constexpr</font> and is annoying boilerplate (and again, 3rd party code breaks that party).</div><div class=""><br class=""></div><div class="">This is also useful for some thread-related use cases: we have empirical evidence that threads using globals cause crashes if these globals are being destroyed.</div><div class=""><br class=""></div><div class="">Thread-local storage is similarly painful for different reasons. I’m not proposing that anything be done yet, but let’s keep it in mind.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Developers want this?</b></div><div class=""><br class=""></div><div class="">Yes, we’ve received numerous requests for this. Developers are worried about code footprint, and have numerous crash reports they’d like to get rid of. Developers tell us they’d really rather not pay for this feature, because they don’t want to use it yet are stuck with it (and C++ is a “don’t pay for what you don’t use” language).</div><div class="">Interesting note: developers are used to having no cleanup on termination on platforms where applications can get terminated when e.g. they’re sent to the background by user action.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">Concrete example</b></div><div class=""><br class=""></div><div class="">Greg Parker provided this example of thread-related issues in the previous discussion:</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><div class="">The Objective-C runtime has a global table that stores retain counts. Pretty much all Objective-C code in the process uses this table. With global destructors in place this table is destroyed during exit(). If any other thread is still running Objective-C code then it will crash.</div></div><div class=""><div class=""><br class=""></div></div><div class=""><div class="">Currently the Objective-C runtime avoids the destructor by initializing this table using placement new into an aligned static char buffer.</div></div></blockquote><div class=""><br class=""></div><div class="">I’m assuming that the embedded usecase is obvious enough to everyone to not need an example. Let me know if that’s not the case.</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><b class="">What about standardization?</b></div><div class=""><br class=""></div><div class="">If this works out I'll discuss standardization options through SG14 (and then WG21). The type of developer who’s asked for this are typical SG14 targets (embedding, gaming, etc). This might fit in with “freestanding” or other similar SG14 efforts, but we need experience to guide the proposal. Maybe EWG will be interested as well? 🤷♂️</div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Thanks,</div><div class=""><br class=""></div><div class="">JF</div></body></html>