Boost logo

Boost Users :

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


Igor,

please take a look at my first post. I wrote there: if you can ensure that
the reference is not saved. This is an essential pitfall. If you pass a
value and bind it to a reference a good compiler will warn you that you are
binding a reference to a temporary object. If you pass the reference no
warnings are issued, but the app crash.

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

> > 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
>
> Well, the problem here is *storing* a reference - not a passing param
> by (const) reference.
>



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