类对象的自动释放(自动清理空间)

类对象的自动释放(自动清理空间)


一.前言

在我们通过new出来一个对象时,可能会忘记delete对象导致内存空间泄露,也可能时程序突然退出等等一些列情况,导致来不及回收空间,所以需要掌握一些关于类自动释放的方法。

二.自动释放对象的方法

这里使用单例模式进行演示:

1.嵌套AutoRelease类并添加静态成员变量

在这里先明白AutoRelease类,因为在类A中_auto是静态变量(且不需要手动delete),所以当程序结束的时候,static修饰的变量生命周期也回终结,此时A因为是new出来的,所以也并没有调用析构函数;将AutoRelease类的析构函数增加delete语句,就可以在AutoRelease类析构的时候也自动析构A类。

#include <iostream>
using std::cout;
using std::endl;


//自动释放对象方法一:嵌套AutoRelease类并添加静态成员变量
class Singleton{
public:
    class AutoRelease{
    public:
        AutoRelease(){
            cout << "AutoRelease()" << endl;
        }
        ~AutoRelease(){
            if (_pInstance){
                delete _pInstance;
                _pInstance = NULL;
            }
            cout << "~AutoRelease()" << endl;
        }
    };

public:
    static Singleton* getInstance()
    {
        return _pInstance;
    }
    void print()const{
        cout << "this is function()" << endl;
    }

private:
    Singleton(){ cout << "Singleton()" << endl;}
    ~Singleton(){cout << "~Singleton()" << endl;}
    static Singleton* _pInstance;
    static AutoRelease _auto;
};


//Singleton* Singleton::_pInstance = NULL;//这是懒汉(饿汉)模式,线程不安全
Singleton* Singleton::_pInstance = new Singleton();//这是饱汉模式,线程安全

Singleton::AutoRelease Singleton::_auto;

int main(){
    Singleton *p = Singleton::getInstance();
    p->print();
    return 0;
}

2.在构造函数中加上atexit函数

atexit函数是在程序结束的时候会执行的函数,在目标类的构造函数中加上atexit函数,这样在程序结束的时候就可以自动调用atexit函数完成清理。

#include <iostream>
#include <unistd.h>
using std::cout;
using std::endl;

//自动释放对象方法二:在构造函数中加上atexit函数

class Singleton
{
public:
    static Singleton *getInstance()
    {
        return _pInstance;
    }

    static void destory()
    {
        if (_pInstance)
        {
            delete _pInstance;
            _pInstance = NULL;
        }
    }

private:
    Singleton()
    {
        atexit(destory);
        cout << "Singleton()" << endl;
    }
    ~Singleton() { cout << "~Singleton()" << endl; }
    static Singleton *_pInstance;
};

//Singleton* Singleton::_pInstance = NULL;//这是懒汉(饿汉)模式,线程不安全
Singleton *Singleton::_pInstance = new Singleton(); //这是饱汉模式,线程安全

int main()
{
    sleep(2);
    return 0;
}

3.pthread_once + atexit (注意此时用的是懒汉模式)
#include <iostream>
#include <unistd.h>
using std::cout;
using std::endl;
pthread_once_t once = PTHREAD_ONCE_INIT;
//自动释放对象方法三:pthread_once + atexit (注意此时用的是懒汉模式)

class Singleton
{
public:
    static Singleton *getInstance()
    {
        pthread_once(&once, initInstance);
        return _pInstance;
    }

    static void initInstance(){
        _pInstance = new Singleton();
        atexit(destory);
    }

    static void destory()
    {
        if (_pInstance)
        {
            delete _pInstance;
            _pInstance = NULL;
        }
    }

private:
    Singleton()
    {
        cout << "Singleton()" << endl;
    }
    ~Singleton() { cout << "~Singleton()" << endl; }
    static Singleton *_pInstance;
};


Singleton* Singleton::_pInstance = NULL;//这是懒汉(饿汉)模式,线程不安全
//Singleton *Singleton::_pInstance = new Singleton(); //这是饱汉模式,线程安全


int main()
{
    Singleton::getInstance();
    return 0;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值