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

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

问题描述

假设这种单例模式的实现(当然我们应该避免使用Singleton:这只是一个问题),我刚刚想到正在创建的静态对象。它是通过一个新的运算符在堆上创建的,但是它是如何被破坏的?在下面的例子中我们有一个泄漏,所以应该如何实现删除静态单例对象?应该有一个 please_delete() public interface,所以可以调用 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(){
// Singleton pattern
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模式根本没有提到删除对象的问题,也许还有其他一些方法?

解决方案

Singleton设计模式的一部分是它是坚不可摧的。



编辑:



有关破坏性的单体有2种:


  1. 可破坏(应用程序死亡)确实)

  2. 坚不可摧(当机器死机时)

无论哪种方式,一旦创建单例实例,它就会保留下来。这是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天全站免登陆