question about boost::intrusive_ptr
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; .... theFeedback->theBPState = &theFetchState[anIndex][!anOne]; The difinition of BranchFeedback and BPredState are: struct BranchFeedback : boost::counted_base { ... boost::intrusive_ptr<BPredState> theBPState; }; struct BPredState : boost::counted_base { ... uint32_t theGShare; uint32_t theSerial; }; The problem is when foo() reaches the end (I mean '}'), I get segmentation fault which points to ~intrusive_ptr() { if(p_ != 0) intrusive_ptr_release(p_); } I thought I used the intrusive_ptr in a correct way. Is there any consideration that I missed? Thanks, // Naderan *Mahmood;
Let me translate some of this, inline:
On Fri, Aug 5, 2011 at 6:38 AM, Mahmood Naderan
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
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. Can you explain more with an example? What do you Mean by "behind the intrusive_ptr's back"?
// Naderan *Mahmood;
----- Original Message -----
From: Gottlob Frege
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 mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Gottlob Frege
-
Mahmood Naderan