about shared_ptr

  • Thread starter Ernst Murnleitner
  • Start date
E

Ernst Murnleitner

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
 
T

tom_usenet

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 said:
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
 
E

Ernst Murnleitner

shared_ptr said:
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?
 
T

tom_usenet

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
 
E

Ernst Murnleitner

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top