body, table{font-family: 微软雅黑; font-size: 10pt}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}

【嵌套类+静态对象】释放
#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\n",Singleton::getInstance());
        cout<<endl;
        Singleton * p1 = Singleton::getInstance();
        Singleton * p2 = Singleton::getInstance();
        printf("p1 = %p\n",p1);
        printf("p2 = %p\n",p2);
        cout<<endl;
        return 0;
}
// 单例类在程序结束时可以显示调用析构函数来释放静态成员变量。但是这样做容易出错,容易忘记。而却也很难保证在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\n",p1);
        printf("p2 = %p\n",p2);
        printf("p3 = %p\n",p3);
        return 0;
}

单例类singleton自动释放的更多相关文章

  1. [转]单例模式——C++实现自动释放单例类的实例

    [转]单例模式——C++实现自动释放单例类的实例 http://www.cnblogs.com/wxxweb/archive/2011/04/15/2017088.html http://blog.s ...

  2. Singleton单例类模式

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  3. [Android面试题-7] 写出一个Java的Singleton类(即单例类)

    1.首先明确单例的概念和特点: a>单例类只能有一个实例 b>单例类必须自己创建一个自己的唯一实例 c>单例类必须为其他所有对象提供这个实例 2.单例具有几种模式,最简单的两种分别是 ...

  4. Unity Singleton 单例类(Unity3D开发之二十)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...

  5. Unity Singleton 单例类(Unity3D开发)

    一.添加单例模板类 using UnityEngine; public class Singleton<T> : MonoBehaviour where T : MonoBehaviour ...

  6. 创建类模式(五):单例(Singleton)

    定义 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式一般情况下通过使用private的构造函数确保了在一个应用中只产生一个实例,并且是自行实例化. 和静态变量的区别 虽然 ...

  7. 设计模式(java) 单例模式 单例类

    ·单例类 单实例类,就是这个类只能创建一个对象,保证了对象实例的唯一性. 1.单例模式( Singleton Pattern) 是一个比较简单的模式, 其定义如下:Ensure a class has ...

  8. (七)boost库之单例类

    (七)boost库之单例类 一.boost.serialzation的单件实现 单例模式是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例类的特殊类.通过单例模式可以保证系统中一个类只有一 ...

  9. 设计模式的征途—1.单例(Singleton)模式

    单例模式属于创建型模式的一种,创建型模式是一类最常用的设计模式,在软件开发中应用非常广泛.创建型模式将对象的创建和使用分离,在使用对象时无需关心对象的创建细节,从而降低系统的耦合度,让设计方案更易于修 ...

随机推荐

  1. jsp 小记

    1. select 默认选中: <select name="skills" multiple="true"> <option value=&q ...

  2. weblogic控制台部署web项目图解

    图解网址:http://jingyan.baidu.com/article/c74d6000650d470f6b595d72.html

  3. 将std::array转换成std::tuple

    template<typename Array, std::size_t... Index> decltype(auto) array2tuple_impl(const Array& ...

  4. asp.net发送短信

    public class SmsServiceManager { public static string Send(string PhoneNumber, out string sendNo) { ...

  5. 谈谈let与const

    let 命令 let命令用于声明变量,但是与传统var命令的不同之处在于拥有以下特性: 使用let命令声明的变量只在let命令所在的代码块内有效(我将之称为变量绑定): 不存在变量提升: 存在暂时性死 ...

  6. [翻译]如何在HTML5中有效使用ARIA

    ARIA是Accessible Rich Internet Application的简称,指无障碍富互联网应用.可以使一些有功能障碍(如听力,视力)的人群,使用你的网站.下面看一下做为开发人员的我们, ...

  7. ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available

    一.笔者使用ifconfig观察网卡情况如下: root@jello:/# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:0 ...

  8. jquery map方法

    jQuery.map( array, callback(elementOfArray, indexInArray) )Returns: Array 感觉jquery的map方法非常好用,特向大家分享下 ...

  9. 爬虫框架Scrapy之Item Pipeline

    Item Pipeline 当Item在Spider中被收集之后,它将会被传递到Item Pipeline,这些Item Pipeline组件按定义的顺序处理Item. 每个Item Pipeline ...

  10. 测试Python类成员的单下划线,双下划线,两头下划线的区别

    首先原谅一个菜鸟叫他“两头下划线”.记得在windows编程中,很多宏定义使用下划线+大写,给人逼格很高的错觉.对于Python下划线的认识,大概是从__dict__这个属性开始的,看__dict__ ...