Boost logo

Boost Users :

From: rogeeff (rogeeff_at_[hidden])
Date: 2002-09-08 17:45:45


--- In Boost-Users_at_y..., "Julia Donawald" <yg-boost-users_at_m...>
wrote:
> Hi,
> the folllowing works not too:
>
> #include <vector>
> #include <shared_ptr.hpp>
>
> class Foo
> {
> public:
> Foo()
> {
> };
> virtual ~Foo()
> {
> };
> };
>
> class Double_Foo
> {
> public:
> Double_Foo()
> {
> };
> Double_Foo(Foo* pFoo1, Foo* pFoo2) : m_Foo1(pFoo1), m_Foo2
(pFoo2)
> {
> };
> virtual ~Double_Foo()
> {
> };
>
> protected:
> typedef boost::shared_ptr<Foo> FooDataPtr;
>
> FooDataPtr m_Foo1;
> FooDataPtr m_Foo2;
> };
>
> int main()
> {
> Foo* pFoo = new Foo();
>
> Double_Foo DoubFoo(pFoo, pFoo);
>
> return 0;
> }
>
>
> I got the same error as in my former post. I read a bit about
shared_ptr.
> Maybe it has something to do with a cyclic, but I thought in
anohter case of
> cyclic data, namely if I have an object A which points to B and B
holds a
> member which points back to A.... that's in my case not?
> If it has something to do with cyclic data then how can I solve it
with
> weak_ptr?
>
> Thanks in advance
> Julia

In both your examples you are getting in the same trouble related to
the fact that you managed to make the same memory mnaged by 2
independent shared_ptr. So it's obvous that when you try to free
memory second time yuo are getting some kind of alarm, depend on
compiler. May be in a form of exception. You should always make sure
that memory is managed by the only smart_ptr. In this case this
would mean hat your code should look like this:

int main()
{
     boost::shared_ptr<Foo> pFoo = new Foo();

     Double_Foo DoubFoo(pFoo, pFoo);

     return 0;
}

And DoubFoo should be changed to accept shared_ptrs ( or const
references ) instead of raw pointers.

Regards,

Gennadiy.


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