• 单例类singleton自动释放


    【嵌套类+静态对象】释放

    #include <iostream>
    #include<stdio.h>
    using namespace std;
    class Singleton
    {
            class AutoRelease;         //前向声明
            private:
                    static Singleton * _pInstance;
                    static AutoRelease _autoRelease;     // 这里只是声明一个变量
                    Singleton();
                    ~Singleton();
                    class AutoRelease
                    {
                            public:
                                    AutoRelease()
                                    {
                                            cout<<"AutoRelease()"<<endl;
                                    }
                                    ~AutoRelease()
                                    {
                                            cout<<"~AutoRelease()"<<endl;
                                            if(_pInstance!=NULL)
                                                    delete _pInstance;
                                    }
                    };
            public:
                    static Singleton * getInstance();
    };
    Singleton * Singleton:: _pInstance = getInstance();
    //Singleton * Singleton:: _pInstance = Singleton::getInstance();
    Singleton::AutoRelease Singleton:: _autoRelease;     //必须要初始化
    //这里才是真正的定义一个变量,类似 int a。
    //非线程安全
    Singleton * Singleton::getInstance()
    {
            cout<<"getInstance()"<<endl;
            if(_pInstance == NULL)
            {
                    _pInstance = new Singleton;
            }
            return _pInstance;
    }

    Singleton::Singleton()
    {
            cout<<"Singleton()"<<endl;
    }

    Singleton::~Singleton()
    {
            cout<<"~Singleton"<<endl;
    }
    【嵌套类+静态对象】释放
    //静态对象函数结束自动调用析构函数释放
    int main()
    {
            cout<<endl;
            cout<<"main()"<<endl;
            printf(" pInstance = %p ",Singleton::getInstance());
            cout<<endl;
            Singleton * p1 = Singleton::getInstance();
            Singleton * p2 = Singleton::getInstance();
            printf("p1 = %p ",p1);
            printf("p2 = %p ",p2);
            cout<<endl;
            return 0;
    }
     // 本来默认是先释放外部类,然后才是嵌套类,由于类是堆对象,不会自动释放,只能靠delete来释放,调用析构函数.


    // 单例类在程序结束时可以显示调用析构函数来释放静态成员变量。但是这样做容易出错,容易忘记。而却也很难保证在delete之后,没有代码再调用析构函数。

    // 最好的方法就是让这个类自己知道在合适的时候把自己删除,程序结束系统会自动释放所有的全局变量,这也包括静态成员变量。可以利用这个特性就可以实现在单例类中定义一个今天成员变量,它的唯一工作就是在析构函数中释放单例类对象。


    【pthread_once + atexit】释放  (和平台相关,用到线程)

    #include <stdlib.h>
    int atexit(void (*function)(void));       //当 main 函数退出时,被注册的函数会自动调用
    #include <iostream>
    #include<stdlib.h>
    using namespace std;
    void func()
    {
            cout<<"func()"<<endl;
    }
    int main()
    {
            cout<<"main()"<<endl;      
            atexit(func);
     //当main退出时,被注册的函数会自动调用
            return 0;
    }

    #include <pthread.h>
    pthread_once_t once_control = PTHREAD_ONCE_INIT;
    int pthread_once ( pthread_once_t  *once_control, void  (*init_routine) (void));       //多次调用,只有第一次调用会执行参数函数

    #include <iostream>
    #include<pthread.h>
    #include<stdio.h>
    #include<stdlib.h>
    using namespace std;
    class Singleton
    {
            private:
                    Singleton();
                    ~Singleton();
                    static Singleton * _pInstance;
                    static pthread_once_t _once;
            public:
                    static Singleton * getInstance();
                    static void init();
                    static void destroy();
    };
    Singleton * Singleton :: _pInstance = NULL;
    pthread_once_t Singleton :: _once = PTHREAD_ONCE_INIT;

    Singleton::Singleton()
    {
            cout<<"Singleton()"<<endl;
    }

    Singleton::~Singleton()
    {
            cout<<"~Singleton()"<<endl;
    }

    void Singleton :: destroy()
    {
            cout<<"destroy()"<<endl;
            if(_pInstance != NULL)
                    delete _pInstance;
    }
    //线程安全
    Singleton * Singleton :: getInstance()
    {
            cout<<"getInstance()"<<endl;
            pthread_once(&_once,Singleton::init);
            return _pInstance;;
    }
    void Singleton::init()
    {
            cout<<"init()"<<endl;
            atexit(Singleton::destroy);
            if(_pInstance == NULL)
            {
                    _pInstance = new Singleton;           //在类里面进行调用
            }
    }
    int main()
    {
            Singleton * p1 = Singleton::getInstance();
            Singleton * p2 = Singleton::getInstance();
            Singleton * p3 = Singleton::getInstance();
            printf("p1 = %p ",p1);
            printf("p2 = %p ",p2);
            printf("p3 = %p ",p3);
            return 0;
    }








  • 相关阅读:
    【web charting】21个Javascript图表插件程序
    【IOC框架】分析与理解
    【待续】【HTML5】用Canvas标签创建第一张条线图
    【转】大型网站后台架构的演变
    初探Visual C# SQL CLR Database Project
    Js中 关于top、clientTop、scrollTop、offsetTop
    JS的正则表达式
    jquery的extend和fn.extend
    C/C++版数据结构之链表<三>
    C/C++ 一点笔记(2)
  • 原文地址:https://www.cnblogs.com/meihao1203/p/8949577.html
Copyright © 2020-2023  润新知