Singleton destruction

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    Singleton destruction

    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


  • John Harrison

    #2
    Re: Singleton destruction


    "Dave" <better_cs_now@ yahoo.com> wrote in message
    news:107b5va2g5 eqm9c@news.supe rnews.com...[color=blue]
    > 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.[/color]

    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;
    }
    };
    [color=blue]
    >
    > 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?[/color]

    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
    [color=blue]
    >
    > Thanks,
    > Dave
    >
    >[/color]


    Comment

    • John Harrison

      #3
      Re: Singleton destruction


      "Dave" <better_cs_now@ yahoo.com> wrote in message
      news:107b5va2g5 eqm9c@news.supe rnews.com...[color=blue]
      > 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.[/color]

      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;
      }
      };
      [color=blue]
      >
      > 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?[/color]

      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
      [color=blue]
      >
      > Thanks,
      > Dave
      >
      >[/color]


      Comment

      • David Harmon

        #4
        Re: Singleton destruction

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

        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.

        Comment

        • David Harmon

          #5
          Re: Singleton destruction

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

          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.

          Comment

          Working...