Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr as reference argument
From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2009-03-19 12:48:13


Yes, that's true. This was coded to model what can happen.

Corrected example could be:

struct Bar
{

  explicit Bar(shared_ptr<int> const& data)
      : data_(data)
  {}

  ~Bar()
  {
      *data_ = 100;
  }

private:
  shared_ptr<int> const& data_;
};

//possible misuse
void foo()
{
     shared_ptr<int> ptr = shared_ptr<int>(new int(10));
     Bar bar(ptr);

     ptr.reset();

} // BOOM!!! => ptr is destroyed before the Bar::~Bar() is called

On Thu, Mar 19, 2009 at 5:34 PM, Igor R <boost.lists_at_[hidden]> wrote:

> > //possible misuse
> > void foo()
> > {
> > Bar bar;
> > shared_ptr<int> ptr = shared_ptr<int>(new int(10));
> >
> > bar.set(ptr);
> > } // BOOM!!! => ptr is destroyed before the Bar::~Bar() is called
> >
> >
> > If you do not program real time soft, it is probably always better to
> copy
> > sp.
>
> In your example, how would the behavior change, if Bar::set would take
> the pram by value?
>
>



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