如何删除单例对象 [英] how to delete singleton object

查看:893
本文介绍了如何删除单例对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设这个单例模式的实现(当然我们应该避免Singleton:它只是问题),我刚刚一直在想静态对象被创建。它是由一个 new 运算符在堆上创建的,但是这是如何销毁的?在下面的示例中,我们有一个泄漏,因此如何实现删除静态单例对象?如果有一个 please_delete()公共接口,所以可以调用 myC-> please_delete()有其他方法来实现这个?

  class CC {
public:
static CC * cObj(){
if c_ptr == NULL){
c_ptr = new CC();
return c_ptr;
} else return c_ptr;
}
int getValue(){return value_;}
void setValue(int val){value_ = val;}
〜CC(){cout< ;}
private:
CC():value_(12345){cout<CC;}
static CC * c_ptr;
int value_;
};
//分配和初始化CC的
//静态数据成员。指针是
//已分配 - 不是对象本身。
CC * CC :: c_ptr = 0;

int main(){
//单例模式
CC * myC = CC :: cObj();
cout<< myC-> getValue();
return 0;
}




输出:CC12345



RUN SUCCESSFUL(总时间:67ms)


我注意到我们总是可以声明singleton static 中的 shared_ptr 中的实例:boost :: shared_ptr< CC> bCptr(CC :: cObj()); 但是Singleton模式没有提到删除对象的问题,所以也许存在一些其他方法?



编辑:

$

b
$ b

关于可破坏性,有两种单例:


  1. 可破坏



  2. 无论如何,正确地,一旦singleton实例被创建,它停留。这是对Singleton设计模式的主要批评之一。



    这里有一些参考文献解决了模式的可破坏性方面。



    http://nicolabonelli.wordpress.com/2009 / 06/04 / singleton-a-mirage-of-perfection /
    http://www10.informatik.uni-erlangen.de/Teaching/Courses/SS2009/CPP/altmann.pdf
    http://sourcemaking.com/design_patterns/singleton
    http://sourcemaking.com / design_patterns / to_kill_a_singleton


    Assuming this implementation of a singleton pattern ( of course we should avoid Singleton: it is just question), I have just been thinking about static object being created. It is created on heap by a new operator, sure, but how is this destroyed? In following example we have a leak, so how one should implement deletion of static singleton object? Should there be a please_delete() public interface adopted, so one can call myC->please_delete() or is there other way to achieve this?

    class CC{
    public:
        static CC* cObj(){
            if(c_ptr==NULL){
                c_ptr=new CC();
                return c_ptr;
            }else return c_ptr;
        }
        int getValue(){return value_;}
        void setValue(int val){value_=val;}
        ~CC(){cout<<"~CC";}
    private:
        CC():value_(12345){cout<<"CC";}
        static CC* c_ptr;
        int value_;
    };
    // Allocating and initializing CC's
    // static data member.  The pointer is being
    // allocated - not the object itself.
    CC *CC::c_ptr = 0;
    
    int main(){
        //Singleton pattern
        CC* myC = CC::cObj();
        cout<<myC->getValue();
        return 0;
    }
    

    output: CC12345

    RUN SUCCESSFUL (total time: 67ms)

    I noticed that indeed we can always declare singleton static instance within shared_ptr as with boost::shared_ptr<CC> bCptr(CC::cObj()); but Singleton pattern doesn't mention the problem of deletion of the object at all so maybe there exists some other approach?

    解决方案

    Part of the Singleton design pattern is that it is indestructible.

    EDIT:

    There are 2 varieties of singletons with respect to destructibility:

    1. Destructible (They die when the application does)
    2. Indestructible (They die when the machine does)

    Either way, if built properly, once the singleton instance is created, it stays. This one of the major criticisms of the Singleton design pattern.

    Here are a few references that address the destructibility aspect of the pattern.

    http://nicolabonelli.wordpress.com/2009/06/04/singleton-a-mirage-of-perfection/ http://www10.informatik.uni-erlangen.de/Teaching/Courses/SS2009/CPP/altmann.pdf http://sourcemaking.com/design_patterns/singleton http://sourcemaking.com/design_patterns/to_kill_a_singleton

    这篇关于如何删除单例对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆