By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,131 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,131 IT Pros & Developers. It's quick & easy.

one more question with smart_ptr

P: n/a
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<BasepBase;
///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<BasepBase;
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

Nov 2 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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<BasepBase;
///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...html#faq-16.8), the
normal reassignment procedure should work fine.

Cheers! --M

Nov 2 '06 #2

P: n/a

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<BasepBase;
///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...html#faq-16.8), the
normal reassignment procedure should work fine.
If I write
auto_ptr<BasepBase;
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<BasepBase1;
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
Nov 3 '06 #3

P: n/a
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<BasepBase;
///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<BasepBase;
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
Nov 3 '06 #4

P: n/a
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
Nov 3 '06 #5

P: n/a

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
Nov 3 '06 #6

P: n/a
toton wrote:
If I write
auto_ptr<BasepBase;
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<BasepBase1;
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

Nov 3 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.