Skip to Content.
Sympa Menu

patterns-discussion - Re: [patterns-discussion] deleting a Singleton

patterns-discussion AT lists.siebelschool.illinois.edu

Subject: General talk about software patterns

List archive

Re: [patterns-discussion] deleting a Singleton


Chronological Thread 
  • From: "Eric Y. Theriault" <eric AT eyt.ca>
  • To: "E. Segura" <esegura2002ar AT yahoo.com.ar>
  • Cc: patterns-discussion AT cs.uiuc.edu
  • Subject: Re: [patterns-discussion] deleting a Singleton
  • Date: Fri, 24 Sep 2004 22:28:05 -0400
  • List-archive: <http://mail.cs.uiuc.edu/pipermail/patterns-discussion>
  • List-id: General talk about software patterns <patterns-discussion.cs.uiuc.edu>
  • Organization: http://www.eyt.ca

E. Segura wrote:

When using the Singleton pattern...

Who should delete the _instance?

Should I ask for it and delete it myself?
( i.e. delete(myClass::getInstance()) ????? )

or the class should keep a "reference count" and other
classes should inform when the don't use the instance
reference anymore.


John Vlissides has written a great article on the topic, entitled "To Kill a Singleton", which I believe you can get at http://www.research.ibm.com/designpatterns/pubs/ph-jun96.txt . Andre Alexandrescu's Modern C++ Design book also has a good treatment of the topic, where he basically allows this to be specified by a policy. In other words, the users of the singleton object should be (more or less) oblivious to how it is constructed and destroyed.

I would avoid the delete mechanism above, since this assumes that the pointer is allocated by new. In actuality, the value returned by getInstance() is not necessarily allocated by new, as Alexandrescu refers to the Meyers Singleton, where the singleton object is a static variable.

The above two resources offer are excellent insight to this issue.



eyt*
--
Eric Y. Theriault
http://www.eyt.ca





Archive powered by MHonArc 2.6.16.

Top of Page