Boost logo

Boost Users :

Subject: Re: [Boost-users] Reference count in shared_ptr
From: Roman Perepelitsa (roman.perepelitsa_at_[hidden])
Date: 2009-10-01 10:47:13


2009/10/1 Daniele Barzotti <daniele.barzotti_at_[hidden]>

> Hi,
>
> I've a doubt about shared_ptr...
> Suppose to have something like this:
>
> template<typename T>
> class Interface {..pure virtual..}
>
> template<typename T>
> class Impl1 : public Interface<T> {....}
>
> template<typename T>
> class Impl2 : public Interface<T> {....}
>
> class A
> {
> public:
> Interface<int>* GetObjPtr() { return &obj; }
>
> private:
> Impl2<int> obj;
> };
>
> class B
> {
> ~B()
> {
> delete a_ptr;
> }
>
> B()
> {
> a_ptr = new A;
> Int_ptr.reset( a_ptr->GetObjPtr );
>

I assume you mean Int_ptr.reset( a_ptr->GetObjPtr() );

> }
>
> private:
> A* a_ptr;
> shared_ptr< Interface<int> > Int_ptr;
> };
>
> Where B creates and use A on its internal.
> In this case, is 'delete' called twice for Impl2?
>

Yes. When you passed a pointer to shared_ptr, you don't own this pointer
anymore, so you shouldn't delete it manually.

Roman Perepelitsa.



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