Boost logo

Boost Users :

From: Hughes, James (jhughes_at_[hidden])
Date: 2007-05-17 10:57:30


snip

> >
> > Sorry, I don't have an answer to your specific question,
> but why would
> > you need to use a shared_ptr on a singleton anyway? You
> have no real
> > need to deallocate a singleton instance, so no need for a
> > shared_ptr....perhaps use a reference (See Meyers singletons)
> >
> > Oh, hold on a sec....is it valid to call get() on an unassigned
> > shared_ptr, which you are doing in the instance function?
> >
> > James
>
> Back when I first learned about the singleton pattern I used
> a standard pointer similar to this example:
>
> class Singleton
> {
> public:
> static Singleton* Instance();
> protected:
> Singleton();
> Singleton(const Singleton&);
> Singleton& operator= (const Singleton&);
> private:
> static Singleton* pinstance;
> };
>
> Now I am trying to be a good programmer and keep track of my pointers.
> So I decided that I should replace the pointer with a shared_ptr.
>
> In regards to the validity of calling get() on an unassigned
> shared_ptr I thought that if a shared_ptr was declared in the
> header and no call is given to any constructor that the
> default constructor would be used.
> Therefore the shared_ptr would have a value of 0x0.
>
> Stephen

Wasn't sure about the get(), but with regard to the singleton itself,
there is no real need to deallocate the singleton instance as the only
memory leak is at the termination of the program and the memory is aonly
allocated once, so there is no need to reference count the people using
the singleton (which would be the purpose of the shared_ptr). I'd just
be inclined to either use your original code, or go with the Meyers
singleton which returns a reference (no pointers at all). Just take care
if multi threaded.

James

This message (including any attachments) contains confidential
and/or proprietary information intended only for the addressee.
Any unauthorized disclosure, copying, distribution or reliance on
the contents of this information is strictly prohibited and may
constitute a violation of law. If you are not the intended
recipient, please notify the sender immediately by responding to
this e-mail, and delete the message from your system. If you
have any questions about this e-mail please notify the sender
immediately.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net