Automatic destruction of instances in a single-case mode of C + + design mode (garbage collector)

Source: Internet
Author: User

About C + + Singleton mode m_pinstance point to the space destruction problem, m_pinstance manual destruction is often a headache problem, memory and resource leakage is not uncommon, can have a method, let the instance automatically release.

The workaround is to define an internal garbage collection class and define a static member of this class in Singleton. At the end of the program, the system automatically destructors this static member, at this time, the destructor in this class to deconstruct the singleton instance, you can implement the M_pinstance automatic release.

Attach the test code

650) this.width= 650, "src="/img/fz.gif "alt=" Copy Code "style=" border:none;margin:0px;padding:0px; "/>

 1  #include  <iostream> 2 using namespace std; 3  4  class Singleton 5 { 6 public: 7     static  Singleton *getinstance ()  8     { 9          if  (m_instance == null) 10          {11             m_Instance =  New singleton ();12             cout< < "get singleton instance success" <<endl;13          }14         return m_Instance;15      }16 17 private:18     singleton () {cout<< "Singleton  construction "<<Endl;} 19     static singleton *m_instance;20 21      // This is important22     class GC //  garbage Collection Class 23      {24     public:25       &NBSP;&NBSP;&NBSP;GC () 26         {27              cout<< "Gc construction" <<endl;28  &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}29&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;~GC () 30         {31              cout<< "Gc destruction" <<endl;32              // We can destory all the  Resouce here, eg:db connector, file handle and so on33              if  (m_instance != null) 34              {35                  delete m_Instance;36                  m_Instance = NULL;37                  cout<< " Singleton destruction "<<endl;38                  system ("pause");//Do not pause the program will automatically exit, can not see the output information 39              }40         }41      };42     static gc gc;  //static members of the garbage collection class 43 44 };45 46 singleton Static members of the  *singleton::m_instance = null;47 singleton::gc singleton::gc; //class require external initialization of the class, This is important, otherwise the program run even GC constructs will not enter, talk about automatic destruction 48 int main (int argc, char *argv[]) 49 {50      singleton *singletonobj = singleton::getinstance ();51      return 0;52 }

650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" border:none;margin:0px;padding:0px; "/>


Operation Result:

650) this.width=650; "src=" http://img.blog.csdn.net/20160516161951962 "style=" border:0px;height:auto;margin:0px; padding:0px; "/>


Automatic destruction of instances in a single-case mode of C + + design mode (garbage collector)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.