Boost logo

Boost Users :

From: Leon Mlakar (leon.mlakar_at_[hidden])
Date: 2007-05-17 10:59:11


> 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.

It would, and you shouldn't get the message. Perhaps the message comes from
the copy assignment (which again, it shouldn't) - try

        m_instance.reset(new Trace_State());

instead.

You could also simply write if (!m_instance) as shared_ptr declares operator
! just for this purpose.

But I agree, in a singleton pattern you benefit from shared pointer only if
the singleton lives shorter than the application and you want to make sure
it's not gone while still in use.

Leon


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