Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > one more question with smart_ptr

Reply
Thread Tools

one more question with smart_ptr

 
 
toton
Guest
Posts: n/a
 
      11-02-2006
Hi,
One more question with smart_ptr ( I am using the one from Axter).
I want to assign the object to the pointer later, not at the
construction time.
like,
smart_ptr<Base> pBase;
///what will be the step here to release the NULL pointer and reduce
the reference count?
pBase = smart_ptr<Base>(new Derived()) ; ///???
Here upon deletion it deletes the Derived twice. Thus I want pBase to
point nowhere and when only it is assigned, the count to increase.

I want it, in case, when the exact derived class is calculated after
some computation, inside constructor (Not in initializer list) and
assigned later.
To give an idea, say the code is a builder like,

class DerivedBuilder{
private:
smart_ptr<Base> pBase;
public:
DerivedBuilder(int type){
if(type < =0) {
pBase = smart_ptr<Base>(new Derived1()) ;
}
else if(type > 0 & <=10){
pBase = smart_ptr<Base>(new Derived3()) ;
}
else{
pBase = smart_ptr<Base>(new Derived2()) ;
}
}
};

Here it is synonymous to boost shared_ptr. I want pBase will call the
destructor only once.
Thanks
abir

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-02-2006
toton wrote:
> One more question with smart_ptr ( I am using the one from Axter).
> I want to assign the object to the pointer later, not at the
> construction time.
> like,
> smart_ptr<Base> pBase;
> ///what will be the step here to release the NULL pointer and reduce
> the reference count?
> pBase = smart_ptr<Base>(new Derived()) ; ///???
> Here upon deletion it deletes the Derived twice. Thus I want pBase to
> point nowhere and when only it is assigned, the count to increase.


No. With any smart pointer that is worthy of the name (e.g.,
std::tr1::shared_ptr, std::auto_ptr, etc.), you don't need to do
anything. The smart pointer will initialize to null (and the reference
count, if there is one, will be 0), and since it's safe to delete a
null pointer
(http://parashift.com/c++-faq-lite/fr...tml#faq-16.8-), the
normal reassignment procedure should work fine.

Cheers! --M

 
Reply With Quote
 
 
 
 
toton
Guest
Posts: n/a
 
      11-03-2006

mlimber wrote:
> toton wrote:
> > One more question with smart_ptr ( I am using the one from Axter).
> > I want to assign the object to the pointer later, not at the
> > construction time.
> > like,
> > smart_ptr<Base> pBase;
> > ///what will be the step here to release the NULL pointer and reduce
> > the reference count?
> > pBase = smart_ptr<Base>(new Derived()) ; ///???
> > Here upon deletion it deletes the Derived twice. Thus I want pBase to
> > point nowhere and when only it is assigned, the count to increase.

>
> No. With any smart pointer that is worthy of the name (e.g.,
> std::tr1::shared_ptr, std::auto_ptr, etc.), you don't need to do
> anything. The smart pointer will initialize to null (and the reference
> count, if there is one, will be 0), and since it's safe to delete a
> null pointer
> (http://parashift.com/c++-faq-lite/fr...tml#faq-16.8-), the
> normal reassignment procedure should work fine.

If I write
auto_ptr<Base> pBase;
pBase = auto_ptr<Base>(new Derived(5));

I get the expected result. Derived destructor & Base destructor is
called only once.

However If I use Axter smart_ptr (which is pretty common and has its
origin on Andrei Alexandrescu 's policy based smart pointers ),
smart_ptr<Base> pBase1;
pBase1 = smart_ptr<Base>(new Derived(5));
I get the destructor gets called twice. For a single line statement it
works fine.
can anyone point to the problem. The Base and Derived class are simple,
just to track emmory, has a cout in ctor and dtor.

Thanks

> Cheers! --M


 
Reply With Quote
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      11-03-2006
toton wrote:

> Hi,
> One more question with smart_ptr ( I am using the one from Axter).
> I want to assign the object to the pointer later, not at the
> construction time.
> like,
> smart_ptr<Base> pBase;
> ///what will be the step here to release the NULL pointer and reduce
> the reference count?
> pBase = smart_ptr<Base>(new Derived()) ; ///???
> Here upon deletion it deletes the Derived twice. Thus I want pBase to
> point nowhere and when only it is assigned, the count to increase.


Are you using the copy_ptr or clone_ptr policies? In that case, there would
be no reference count. Instead each pointer has its own unique pointee and
upon pointer-assignments, such pointees get deep-copied. Thus, when you see
a double destruction, it is actually two different objects that are
destroyed.

> I want it, in case, when the exact derived class is calculated after
> some computation, inside constructor (Not in initializer list) and
> assigned later.
> To give an idea, say the code is a builder like,
>
> class DerivedBuilder{
> private:
> smart_ptr<Base> pBase;
> public:
> DerivedBuilder(int type){
> if(type < =0) {
> pBase = smart_ptr<Base>(new Derived1()) ;
> }
> else if(type > 0 & <=10){
> pBase = smart_ptr<Base>(new Derived3()) ;
> }
> else{
> pBase = smart_ptr<Base>(new Derived2()) ;
> }
> }
> };
>
> Here it is synonymous to boost shared_ptr. I want pBase will call the
> destructor only once.


That is because shared_ptr has a reference count and does not do deep-copy.


Best

Kai-Uwe Bux
 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      11-03-2006
On 2 Nov 2006 22:11:16 -0800, "toton" wrote:
>can anyone point to the problem.


Avoid 'smart pointers' and your 'smart pointer' related problems
disappear immediately.

Good luck,
Roland Pibinger
 
Reply With Quote
 
toton
Guest
Posts: n/a
 
      11-03-2006

Roland Pibinger wrote:
> On 2 Nov 2006 22:11:16 -0800, "toton" wrote:
> >can anyone point to the problem.

>
> Avoid 'smart pointers' and your 'smart pointer' related problems
> disappear immediately.
>
> Good luck,

If you have headache, will you cut your head!
How strange people can be!
> Roland Pibinger


 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      11-03-2006
toton wrote:
> If I write
> auto_ptr<Base> pBase;
> pBase = auto_ptr<Base>(new Derived(5));
>
> I get the expected result. Derived destructor & Base destructor is
> called only once.


Right because there's only one object and ownership is passed from one
auto_ptr to the other. In the case of shared_ptr with the same code,
there's still only one Base/Derived object in play, and the reference
count for that object could change (depending on your compiler's
optimization capabilities -- it might not exceed 1).

> However If I use Axter smart_ptr (which is pretty common and has its
> origin on Andrei Alexandrescu 's policy based smart pointers ),
> smart_ptr<Base> pBase1;
> pBase1 = smart_ptr<Base>(new Derived(5));
> I get the destructor gets called twice. For a single line statement it
> works fine.
> can anyone point to the problem. The Base and Derived class are simple,
> just to track emmory, has a cout in ctor and dtor.


See Kai-Uwe Bux's response elsethread.

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can one declare more than one signal on one line? Merciadri Luca VHDL 4 11-01-2010 02:00 PM
compiler gives me error message " boost/smart_ptr.h: No such file ordirectory" Pallav singh C++ 4 04-29-2009 08:01 AM
smart_ptr question toton C++ 8 11-02-2006 07:00 AM
Kamaelia 0.4.0 RELEASED - Faster! More Tools! More Examples! More Docs! ;-) Michael Python 4 06-26-2006 08:00 AM
boost smart_ptr and WIN32 PostMessage figsys@yahoo.com C++ 3 06-20-2006 09:29 PM



Advertisments