about shared_ptr

Discussion in 'C++' started by Ernst Murnleitner, Jan 8, 2004.

  1. 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
     
    Ernst Murnleitner, Jan 8, 2004
    #1
    1. Advertising

  2. Ernst Murnleitner

    tom_usenet Guest

    On Thu, 8 Jan 2004 18:56:45 +0100, "Ernst Murnleitner"
    <> 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
     
    tom_usenet, Jan 8, 2004
    #2
    1. Advertising

  3. > >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?
     
    Ernst Murnleitner, Jan 9, 2004
    #3
  4. Ernst Murnleitner

    tom_usenet Guest

    On Fri, 9 Jan 2004 13:11:56 +0100, "Ernst Murnleitner"
    <> 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
     
    tom_usenet, Jan 9, 2004
    #4
  5. 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
     
    Ernst Murnleitner, Jan 12, 2004
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. SerGioGio

    shared_ptr vs std::auto_ptr

    SerGioGio, Jul 3, 2003, in forum: C++
    Replies:
    3
    Views:
    5,449
    Alexander Terekhov
    Jul 3, 2003
  2. Philippe Guglielmetti
    Replies:
    4
    Views:
    900
    tom_usenet
    Oct 9, 2003
  3. James Mastro

    boost::shared_ptr inside stl::list

    James Mastro, Nov 13, 2003, in forum: C++
    Replies:
    1
    Views:
    2,038
    Cy Edmunds
    Nov 13, 2003
  4. Derek
    Replies:
    2
    Views:
    627
    Derek
    Dec 8, 2003
  5. Colin Caughie
    Replies:
    1
    Views:
    717
    Shooting
    Aug 29, 2006
Loading...

Share This Page