{"id":999,"date":"2012-10-02T14:38:16","date_gmt":"2012-10-02T14:38:16","guid":{"rendered":"http:\/\/kera.name\/articles\/?p=999"},"modified":"2012-10-02T14:38:16","modified_gmt":"2012-10-02T14:38:16","slug":"auto_ptr-and-forward-declarations-basically-dont","status":"publish","type":"post","link":"https:\/\/kera.name\/articles\/2012\/10\/auto_ptr-and-forward-declarations-basically-dont\/","title":{"rendered":"Auto_ptr and forward declarations: basically, don&#039;t"},"content":{"rendered":"<p>I made a mistake that could have cost my application its stability. Learn from the error of my ways!<\/p>\n<hr \/>\n<p>Cleaning up some code, I saw an opportunity for a forward declaration and I took it:<\/p>\n<pre><code>struct T2;\n\nstruct T1\n{\n    std::auto_ptr<T2> obj;\n};<\/code><\/pre>\n<p>Only by chance, some hours later, did I spot a diagnostic note from GCC hidden away in my build output:<\/p>\n<blockquote>\n<p>note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.<\/p>\n<\/blockquote>\n<p>Deleting an object of an incomplete type yields <em>undefined behaviour<\/em>, and since obviously the standard library runtime implementing <code>std::auto_ptr<\/code> does not define my type <code>T2<\/code>, the destruction of an <code>std::auto_ptr<T2><\/code> now invokes this UB.<\/p>\n<p>I was lucky to spot this &#8212; I figure GCC should emit a warning rather than a note, since I&#039;m trained to spot them from a mile away. In the meantime, that&#039;s one mystical shutdown-time segfault (that I&#039;d shelved for later investigation, not considering that it could be linked to this code change) explained.<\/p>\n<pre><code>#include \"T2.h\"\n\nstruct T1\n{\n    std::auto_ptr<T2> obj;\n};<\/code><\/pre>\n<p>I lose my forward declaration, but at least my program is well-defined and will not open black holes. So there are kind of some pluses and minuses.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I made a mistake that could have cost my application its stability. Learn from the error of my ways!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[21,12],"_links":{"self":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/999"}],"collection":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/comments?post=999"}],"version-history":[{"count":2,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/999\/revisions"}],"predecessor-version":[{"id":1001,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/posts\/999\/revisions\/1001"}],"wp:attachment":[{"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/media?parent=999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/categories?post=999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kera.name\/articles\/wp-json\/wp\/v2\/tags?post=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}