Singleton destruction

Discussion in 'C++' started by Dave, Apr 8, 2004.

  1. Dave

    Dave Guest

    Hello all,

    I have made a class into a singleton by having a static get() method which
    returns a pointer to the one-and-only instance. This method creates the
    instance on the heap if it hasn't already done so in a prior call.
    Constructors are private so that the class may not be instaniated from
    outside. As I understand it, this is the standard approach and is nothing
    new.

    I have no need to ever destroy this singleton and re-create a new one in
    it's place, so I have not provided a method for destruction.

    My question is: Where in my code, if ever, should the singleton be
    deallocated? Is it standard practice to just ignore this memory, or would
    it really be more proper to find some way to deallocate it manually upon
    app. exit?

    Thanks,
    Dave
     
    Dave, Apr 8, 2004
    #1
    1. Advertising

  2. "Dave" <> wrote in message
    news:...
    > Hello all,
    >
    > I have made a class into a singleton by having a static get() method which
    > returns a pointer to the one-and-only instance. This method creates the
    > instance on the heap if it hasn't already done so in a prior call.
    > Constructors are private so that the class may not be instaniated from
    > outside. As I understand it, this is the standard approach and is nothing
    > new.


    There is another approach, declare the object as static within the get
    method. This ensures that it will be destructed on exit.

    class X
    {
    static X& get()
    {
    static X the_x;
    return the_x;
    }
    };

    >
    > I have no need to ever destroy this singleton and re-create a new one in
    > it's place, so I have not provided a method for destruction.
    >
    > My question is: Where in my code, if ever, should the singleton be
    > deallocated? Is it standard practice to just ignore this memory, or would
    > it really be more proper to find some way to deallocate it manually upon
    > app. exit?


    Depends on how good your OS is on reclaiming resources from exited
    applications. I don't think operating systems have much problem with memory,
    that will automatically be reclaimed. Other resources, well I think you have
    to consult your documentation.

    The best discussion I know of singletons and the policies you can adopt for
    them is in Andrei Alexandrescu's book Modern C++ Design. You can get the
    code if not the book online, google for loki library.

    john

    >
    > Thanks,
    > Dave
    >
    >
     
    John Harrison, Apr 8, 2004
    #2
    1. Advertising

  3. Dave

    David Harmon Guest

    On Thu, 8 Apr 2004 19:30:52 +0100 in comp.lang.c++, "John Harrison"
    <> wrote,
    >There is another approach, declare the object as static within the get
    >method. This ensures that it will be destructed on exit.


    And if something requires that the object be allocated with new, that's
    a case where a static std::auto_ptr<t> can do the job.
     
    David Harmon, Apr 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,253
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    166
  3. Trans
    Replies:
    12
    Views:
    280
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    206
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    203
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page