Boost logo

Boost Users :

Subject: Re: [Boost-users] question about boost::intrusive_ptr
From: Gottlob Frege (gottlobfrege_at_[hidden])
Date: 2011-08-06 02:13:42


Let me translate some of this, inline:

On Fri, Aug 5, 2011 at 6:38 AM, Mahmood Naderan <nt_mahmood_at_[hidden]> wrote:
> Hi
> I used boost::intrusive_ptr in a function, say foo(), like this:
>
>     boost::intrusive_ptr<BranchFeedback> theFeedback ( new BranchFeedback() );
>     std::vector< std::vector< BPredState > >           theFetchState;

You have some container of BPredState objects. The container holds -
manages - the objects, not just intrusive_ptrs to the objects.
The objects have internal reference counts, that are probably each
initially set to 0.

>     ....
>     theFeedback->theBPState = &theFetchState[anIndex][!anOne];
>

you set an intrusive_ptr to the raw pointer of one of the BPredState
objects inside the container. Its ref count probably goes to 1.

>
> The problem is when foo() reaches the end (I mean '}'), I get segmentation fault which points to

At the end of foo(), your container of objects is destroyed. It
deletes all the BPredState objects.
Regardless of their reference counts.
You asked the container to manage those objects, not just
intrusive_ptrs to the objects, so when the container is done, the
objects are gone.

>     ~intrusive_ptr()
>     {
>         if(p_ != 0) intrusive_ptr_release(p_);
>     }
>

the intrusive_ptr to one of the BPredObjects is then destroyed. It is
currently pointing to an already destroyed BPredObject. It tries to
decrement the ref count, probably setting it to 0. It tries to delete
the already deleted object...

> I thought I used the intrusive_ptr in a correct way. Is there any consideration that I missed?
> Thanks,
>

If you are managing via intrusive_ptrs, then make sure ALL management
is done via intrusive_ptrs. Don't delete the objects from behind the
intrusive_ptr's back.

Tony


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