{"id":243,"date":"2009-03-24T14:43:06","date_gmt":"2009-03-24T19:43:06","guid":{"rendered":"http:\/\/www.virtualroadside.com\/blog\/?p=243"},"modified":"2009-03-24T14:50:31","modified_gmt":"2009-03-24T19:50:31","slug":"another-solution-to-the-singleton-pattern-in-c","status":"publish","type":"post","link":"http:\/\/www.virtualroadside.com\/blog\/index.php\/2009\/03\/24\/another-solution-to-the-singleton-pattern-in-c\/","title":{"rendered":"Another solution to the Singleton pattern in C++"},"content":{"rendered":"<p>So, while working on the <a href=\"http:\/\/www.virtualroadside.com\/FRC\/#Kwarqs2009\">Kwarqs robotics code<\/a>, I found a useful solution to implementing the singleton pattern in c++ that I believe avoids the <a href=\"http:\/\/www.parashift.com\/c++-faq-lite\/ctors.html#faq-10.13\">static initialization fiasco<\/a> (with a small restriction) *and* doesn&#8217;t leak memory.<\/p>\n<p>What I&#8217;ve created is a template class that takes a pointer to a pointer at construction, and deletes the pointer as it is getting destructed. While you cannot guarantee when the class is created due to static initialization issues, from <a href=\"http:\/\/www.parashift.com\/c++-faq-lite\/intrinsic-types.html#faq-26.9\">what I can see<\/a> the C++ standard does guarantee that all intrinsics are initialized before other items are constructed. By using a pointer to a pointer, we can guarantee that the location of the object will be the same (and thus destruction will happen correctly), and provided we initialize the static variable to NULL we can also guarantee that initialization will happen correctly.<\/p>\n<p><strong>Warning<\/strong>: A key assumption made by this class is that the static object is only accessed during the normal scope of the program &#8212; ie, the object that this class contains a pointer to a pointer to should not be accessed by anything that could be called after main() is complete. For some code bases, this can be very tricky to enforce.<\/p>\n<p>Usage of the class is quite trivial:<\/p>\n<pre>\r\nSomeType * SomeType::m_instance = NULL;\r\nStaticDeleter<SomeType> someTypeDeleter(&m_instance);\r\n\r\n\/\/ example singleton initialization\r\nSomeType * SomeType::GetInstance()\r\n{\r\n\tif (SomeType::m_instance == NULL)\r\n\t\tSomeType::m_instance = new SomeType();\r\n\treturn SomeType::m_instance;\r\n}\r\n\r\n<\/pre>\n<p>And the implementation of the static deleter is also quite trivial (but I&#8217;ve provided a downloadable version as well), check it out:<\/p>\n<pre>\r\n\/\/ (C) 2009 Dustin Spicuzza, released under modified BSD license\r\n\r\ntemplate <typename TypeToDelete>\r\nclass StaticDeleter {\r\npublic:\r\n\r\n    StaticDeleter(TypeToDelete ** ptr2ptr) :\r\n        m_ptr2ptr(ptr2ptr)\r\n    {}\r\n\r\n    ~StaticDeleter()\r\n    {\r\n        if (*m_ptr2ptr)\r\n            delete *m_ptr2ptr;\r\n    }\r\n\r\nprivate:\r\n    TypeToDelete ** m_ptr2ptr;\r\n};<\/pre>\n<p><a href=\"http:\/\/www.virtualroadside.com\/software\/#StaticDeleter\">Download it now<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, while working on the Kwarqs robotics code, I found a useful solution to implementing the singleton pattern in c++ that I believe avoids the static initialization fiasco (with a small restriction) *and* doesn&#8217;t leak memory. What I&#8217;ve created is a template class that takes a pointer to a pointer at construction, and deletes the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/243"}],"collection":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":6,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"predecessor-version":[{"id":249,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/243\/revisions\/249"}],"wp:attachment":[{"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.virtualroadside.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}