about shared_ptr

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ernst Murnleitner

    about shared_ptr

    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_p tr).
    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_thi s():
    template enable_shared_f rom_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_s hared_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






  • tom_usenet

    #2
    Re: about shared_ptr

    On Thu, 8 Jan 2004 18:56:45 +0100, "Ernst Murnleitner"
    <mur-spam@awite.de> wrote:
    [color=blue]
    >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;[/color]

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

    shared_ptr<A> a(new A);
    [color=blue]
    >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?[/color]

    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.
    [color=blue]
    >2) shared_from_thi s():
    >template enable_shared_f rom_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_s hared_from_this <> as
    >Base again, I cannot compile (ambigous...).[/color]

    Right.
    [color=blue]
    >
    >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).[/color]

    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).
    [color=blue]
    >I know that 2 base classes is not a good idea, but there was a problem by
    >using virtual inheritance.[/color]

    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

    Comment

    • Ernst Murnleitner

      #3
      Re: about shared_ptr

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

      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?







      Comment

      • tom_usenet

        #4
        Re: about shared_ptr

        On Fri, 9 Jan 2004 13:11:56 +0100, "Ernst Murnleitner"
        <mur-spam@awite.de> wrote:
        [color=blue][color=green][color=darkred]
        >> >shared_ptr<A > a = new A;[/color]
        >>
        >> Don't do the above - the code won't compile on a conforming compiler.
        >> Instead, do:
        >>
        >> shared_ptr<A> a(new A);
        >>[/color]
        >
        >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?[/color]

        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

        Comment

        • Ernst Murnleitner

          #5
          Re: about shared_ptr

          boost::shared_p tr 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::intrusiv e_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






          Comment

          Working...