Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > about shared_ptr

Reply
Thread Tools

about shared_ptr

 
 
Ernst Murnleitner
Guest
Posts: n/a
 
      01-08-2004
Hello,

in 2 other threads I had questions partly related to shared_ptr.

I changed my normal pointers to a class to shared_ptr. (i.e.
boost::shared_ptr).
I thought, the use of shared_ptr is more save. But after this my program
crashed by several reasons (see below). But maybe there would be an easy
solution which I cannot see now.

1) I mixed normal pointers and shared _ptr at some places. This is very
ugly, for example:

class A;

shared_ptr<A> a = new A;
A * p = a.this();
{
shared_ptr<A> a2 = p;
} // the object will be deleted here!!!
a->f(); // crash?


Question: Is it possible to make that a shared_ptr can only be initialised
by another shared_ptr or by the new expression?


2) shared_from_this():
template enable_shared_from_this cannot be used in a class with 2 instances
of the same base class?

In my hierarchy, I have the same base class twice in one object
(non-virtual). As this base class has boost::enable_shared_from_this<> as
Base again, I cannot compile (ambigous...).

Maybe I could circumvent this by making one Baseclass a virtual base class.
But wasn't there a restriction by using virtual base classes? (As far as I
can remember it was something with RTTI or with casts).

I know that 2 base classes is not a good idea, but there was a problem by
using virtual inheritance.

Greetings
Ernst






 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      01-08-2004
On Thu, 8 Jan 2004 18:56:45 +0100, "Ernst Murnleitner"
<(E-Mail Removed)> wrote:

>Hello,
>
>in 2 other threads I had questions partly related to shared_ptr.
>
>I changed my normal pointers to a class to shared_ptr. (i.e.
>boost::shared_ptr).
>I thought, the use of shared_ptr is more save. But after this my program
>crashed by several reasons (see below). But maybe there would be an easy
>solution which I cannot see now.
>
>1) I mixed normal pointers and shared _ptr at some places. This is very
>ugly, for example:
>
>class A;
>
>shared_ptr<A> a = new A;


Don't do the above - the code won't compile on a conforming compiler.
Instead, do:

shared_ptr<A> a(new A);

>A * p = a.this();
>{
>shared_ptr<A> a2 = p;
>} // the object will be deleted here!!!
>a->f(); // crash?
>
>
>Question: Is it possible to make that a shared_ptr can only be initialised
>by another shared_ptr or by the new expression?


No, but using a conforming compiler prevents most mistakes (due to the
explicit T* constructor). Initializing a shared_ptr with a pointer
that hasn't just been newed in the same expression is almost always a
mistake, but if you know that, it's a hard mistake to make. Unless
your compiler has a bug that enables the implicit conversion, in which
case it is quite an easy mistake.

>2) shared_from_this():
>template enable_shared_from_this cannot be used in a class with 2 instances
>of the same base class?
>
>In my hierarchy, I have the same base class twice in one object
>(non-virtual). As this base class has boost::enable_shared_from_this<> as
>Base again, I cannot compile (ambigous...).


Right.

>
>Maybe I could circumvent this by making one Baseclass a virtual base class.
>But wasn't there a restriction by using virtual base classes? (As far as I
>can remember it was something with RTTI or with casts).


Virtual inheritence has a small amount of overhead associated with it,
but no other problems that I can think of (other than having to
initialize the virtual base in the most derived class).

>I know that 2 base classes is not a good idea, but there was a problem by
>using virtual inheritance.


You should use virtual inheritence unless you really want two copies
of the base class (which I doubt).

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
 
 
 
Ernst Murnleitner
Guest
Posts: n/a
 
      01-09-2004
> >shared_ptr<A> a = new A;
>
> Don't do the above - the code won't compile on a conforming compiler.
> Instead, do:
>
> shared_ptr<A> a(new A);
>


I think, in most cases, shared_ptr<A> a(new A) is not applicable, as
pointers are mostly used, if the class has to be constructed at runtime. If
I could make the initialisation this way, I would not need shared_ptr at
all:

In the class I defined a member variable
shared_ptr<A> a;

In the constructor, depending on the configuration stored in a data file:
a = new A;

Maybe I had to use
a = shared_ptr<A> (new A);
for compilers other than gcc 2.95?







 
Reply With Quote
 
tom_usenet
Guest
Posts: n/a
 
      01-09-2004
On Fri, 9 Jan 2004 13:11:56 +0100, "Ernst Murnleitner"
<(E-Mail Removed)> wrote:

>> >shared_ptr<A> a = new A;

>>
>> Don't do the above - the code won't compile on a conforming compiler.
>> Instead, do:
>>
>> shared_ptr<A> a(new A);
>>

>
>I think, in most cases, shared_ptr<A> a(new A) is not applicable, as
>pointers are mostly used, if the class has to be constructed at runtime. If
>I could make the initialisation this way, I would not need shared_ptr at
>all:
>
>In the class I defined a member variable
>shared_ptr<A> a;
>
>In the constructor, depending on the configuration stored in a data file:
>a = new A;
>
>Maybe I had to use
>a = shared_ptr<A> (new A);
>for compilers other than gcc 2.95?


Yes, you do. This bug in 2.95 makes using shared_ptr unnecessarily
hazardous!

Tom

C++ FAQ: http://www.parashift.com/c++-faq-lite/
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
 
Reply With Quote
 
Ernst Murnleitner
Guest
Posts: n/a
 
      01-12-2004
boost::shared_ptr did not work, as I needed a pointer to "this" already in
the constructor. As I found no way to fix this, I finally used
boost::intrusive_ptr.

As my classes are inherited from a base class, intrusive_ptr was a very good
solution. I can use the address operator for assignment to the
intrusive_ptr, and I can also assign "this" to the pointer at any time.
(Reference counts are incremented/decremented in the base class).

Greetings
Ernst






 
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
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
boost::shared_ptr and operator->() Derek C++ 2 12-08-2003 05:13 PM
boost::shared_ptr inside stl::list James Mastro C++ 1 11-13-2003 01:59 AM
error with boost::shared_ptr<T> with incomplete T, VC7.1 ? Philippe Guglielmetti C++ 4 10-09-2003 05:30 PM
shared_ptr vs std::auto_ptr SerGioGio C++ 3 07-03-2003 02:52 PM



Advertisments