Preventing delete on a SmartPointer

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

    Preventing delete on a SmartPointer

    Hi,

    I have implemented a SmartPointer class following the implementation
    proposed by Bill Hubauer(*). But I also override the operator * ()

    template<class ObjectType>
    class SmartPointer
    {
    public:
    operator ObjectType * () const
    { return Pointer; }
    ....
    private:
    ObjectType* Pointer;
    };

    Now my compiler can compile:

    SmartPointer<Ob ject> s = new Object;
    delete s;

    Is there a way to prevent that ?

    Thanks,
    Mathieu

    (*)


    Full source code is at:


  • Daniel T.

    #2
    Re: Preventing delete on a SmartPointer

    In article <1150503441.134 399.221640@h76g 2000cwa.googleg roups.com>,
    "mathieu" <mathieu.malate rre@gmail.com> wrote:
    [color=blue]
    > Hi,
    >
    > I have implemented a SmartPointer class following the implementation
    > proposed by Bill Hubauer(*). But I also override the operator * ()
    >
    > template<class ObjectType>
    > class SmartPointer
    > {
    > public:
    > operator ObjectType * () const
    > { return Pointer; }
    > ...
    > private:
    > ObjectType* Pointer;
    > };
    >
    > Now my compiler can compile:
    >
    > SmartPointer<Ob ject> s = new Object;
    > delete s;
    >
    > Is there a way to prevent that ?[/color]

    Yes. If you don't explicitly tell the compiler that this is allowed, it
    won't allow it. That means remove the operator ObjectType*().

    Comment

    • Phlip

      #3
      Re: Preventing delete on a SmartPointer

      Daniel T. wrote:
      [color=blue][color=green]
      >> SmartPointer<Ob ject> s = new Object;
      >> delete s;
      >>
      >> Is there a way to prevent that ?[/color]
      >
      > Yes. If you don't explicitly tell the compiler that this is allowed, it
      > won't allow it. That means remove the operator ObjectType*().[/color]

      Of course that fix is the best style. (It's why the C++ Standard Library has
      few if no conversion operators.)

      But can't the templated class also provide an operator delete? Then it could
      correctly zilch its owning pointer.

      --
      Phlip
      http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!


      Comment

      • mathieu

        #4
        Re: Preventing delete on a SmartPointer


        Daniel T. wrote:[color=blue]
        > Yes. If you don't explicitly tell the compiler that this is allowed, it
        > won't allow it. That means remove the operator ObjectType*().[/color]

        Well there is -at least- another way like defining the ~Object in the
        protected section, but doing so would prevent me to define an Object on
        the stack.
        I thought there could be another way...

        -M

        Comment

        • Alf P. Steinbach

          #5
          Re: Preventing delete on a SmartPointer

          * Phlip:[color=blue]
          > Daniel T. wrote:
          >[color=green][color=darkred]
          >>> SmartPointer<Ob ject> s = new Object;
          >>> delete s;
          >>>
          >>> Is there a way to prevent that ?[/color]
          >> Yes. If you don't explicitly tell the compiler that this is allowed, it
          >> won't allow it. That means remove the operator ObjectType*().[/color]
          >
          > Of course that fix is the best style. (It's why the C++ Standard Library has
          > few if no conversion operators.)
          >
          > But can't the templated class also provide an operator delete? Then it could
          > correctly zilch its owning pointer.[/color]

          An 'operator delete' in SmartPointer would be used in an expression like

          delete &s;

          not in

          delete s;


          --
          A: Because it messes up the order in which people normally read text.
          Q: Why is it such a bad thing?
          A: Top-posting.
          Q: What is the most annoying thing on usenet and in e-mail?

          Comment

          • Roland Pibinger

            #6
            Re: Preventing delete on a SmartPointer

            On 16 Jun 2006 17:56:44 -0700, "mathieu" <mathieu.malate rre@gmail.com>
            wrote:[color=blue]
            >Daniel T. wrote:[color=green]
            >> Yes. If you don't explicitly tell the compiler that this is allowed, it
            >> won't allow it. That means remove the operator ObjectType*().[/color]
            >
            >Well there is -at least- another way like defining the ~Object in the
            >protected section, but doing so would prevent me to define an Object on
            >the stack. I thought there could be another way...[/color]

            It's yet another "smart" pointers pitfall. Just don't use "smart"
            pointers and the 'problems' are gone.

            Best wishes,
            Roland Pibinger

            Comment

            • benben

              #7
              Re: Preventing delete on a SmartPointer

              mathieu wrote:[color=blue]
              > Daniel T. wrote:[color=green]
              >> Yes. If you don't explicitly tell the compiler that this is allowed, it
              >> won't allow it. That means remove the operator ObjectType*().[/color]
              >
              > Well there is -at least- another way like defining the ~Object in the
              > protected section, but doing so would prevent me to define an Object on
              > the stack.
              > I thought there could be another way...
              >
              > -M
              >[/color]

              Unfortunately you can't make your smart pointer completely fool-proof.
              At the very least one can still do the following:

              SmartPointer<in t> sp(new int(1));
              delete sp.operator->();

              Ben

              Comment

              • Markus Schoder

                #8
                Re: Preventing delete on a SmartPointer

                benben wrote:[color=blue]
                > mathieu wrote:[color=green]
                >> Daniel T. wrote:[color=darkred]
                >>> Yes. If you don't explicitly tell the compiler that this is allowed, it
                >>> won't allow it. That means remove the operator ObjectType*().[/color]
                >>
                >> Well there is -at least- another way like defining the ~Object in the
                >> protected section, but doing so would prevent me to define an Object on
                >> the stack.
                >> I thought there could be another way...
                >>
                >> -M
                >>[/color]
                >
                > Unfortunately you can't make your smart pointer completely fool-proof.
                > At the very least one can still do the following:
                >
                > SmartPointer<in t> sp(new int(1));
                > delete sp.operator->();[/color]

                Yes, and will all know how easily that accidentally happens :)

                To shoot yourself in the foot with less typing do

                delete &*sp;

                Comment

                • mangesh

                  #9
                  Re: Preventing delete on a SmartPointer


                  override delete operatpr in your class and make it private .

                  Regards
                  Mangesh

                  Comment

                  • mangesh

                    #10
                    Re: Preventing delete on a SmartPointer


                    override delete operatpr in your class and make it private .

                    Regards
                    Mangesh

                    Comment

                    • Michiel.Salters@tomtom.com

                      #11
                      Re: Preventing delete on a SmartPointer

                      mathieu wrote:[color=blue]
                      > Hi,
                      >
                      > I have implemented a SmartPointer class following the implementation
                      > proposed by Bill Hubauer(*). But I also override the operator * ()
                      >
                      > template<class ObjectType>
                      > class SmartPointer
                      > {
                      > public:
                      > operator ObjectType * () const
                      > { return Pointer; }
                      > ...
                      > private:
                      > ObjectType* Pointer;
                      > };
                      >
                      > Now my compiler can compile:
                      >
                      > SmartPointer<Ob ject> s = new Object;
                      > delete s;
                      >
                      > Is there a way to prevent that ?[/color]

                      IIRC, the reason it can delete that is because there is exactly one
                      operator that
                      returns a pointer-to-object. If you had a private operator void*, the
                      compiler couldn't
                      determine which pointer type to cast to. This is good; the only case in
                      which you
                      want operator ObjectType* to work is the case where you need an
                      ObjectType*.
                      If the conversion context is ambiguous, like 'delete s', the compiler
                      should tell
                      you about the ambiguity.

                      HTH,
                      Michiel Salters

                      Comment

                      • Joe Seigh

                        #12
                        Re: Preventing delete on a SmartPointer

                        mathieu wrote:[color=blue]
                        > Hi,
                        >
                        > I have implemented a SmartPointer class following the implementation
                        > proposed by Bill Hubauer(*). But I also override the operator * ()
                        >
                        > template<class ObjectType>
                        > class SmartPointer[/color]
                        ....[color=blue]
                        >
                        > Now my compiler can compile:
                        >
                        > SmartPointer<Ob ject> s = new Object;
                        > delete s;
                        >
                        > Is there a way to prevent that ?[/color]

                        I may have missed something here. If you haven't overridden
                        delete then it won't do anything since s is just an object,
                        not a pointer to a dynamically allocated object, as far as
                        C++ is concerned. The object your smart pointer "refers" to
                        won't get deallocated unless your smart pointer implementation
                        decides to do so.


                        --
                        Joe Seigh

                        When you get lemons, you make lemonade.
                        When you get hardware, you make software.

                        Comment

                        • mlimber

                          #13
                          Re: Preventing delete on a SmartPointer

                          Roland Pibinger wrote:[color=blue]
                          > It's yet another "smart" pointers pitfall. Just don't use "smart"
                          > pointers and the 'problems' are gone.[/color]

                          Better yet, go back to assembly, and then all the manifold problems
                          with high level languages disappear!

                          JK

                          Cheers! --M

                          Comment

                          • Peter

                            #14
                            Re: Preventing delete on a SmartPointer


                            mathieu wrote:
                            [color=blue]
                            > template<class ObjectType>
                            > class SmartPointer
                            > {
                            > public:
                            > operator ObjectType * () const
                            > { return Pointer; }
                            > ...
                            > private:
                            > ObjectType* Pointer;
                            > };
                            >
                            > Now my compiler can compile:
                            >
                            > SmartPointer<Ob ject> s = new Object;
                            > delete s;[/color]

                            1)
                            your object class should have only private constructors and one or more
                            public static methods returning a smartPtr.
                            2)
                            Or instead of the static method you could define a factory class which
                            creates instances of your class (by returning a smartptr).
                            3)
                            You could make the destructor of Object private so nobody can call
                            delete on your object pointer.
                            I assume that your smartPtr is calling Addref() and Release() or
                            something similar on your object to increase/decrease the reference
                            count of your object.

                            Comment

                            • Peter

                              #15
                              Re: Preventing delete on a SmartPointer


                              mathieu wrote:[color=blue]
                              > Daniel T. wrote:[color=green]
                              > > Yes. If you don't explicitly tell the compiler that this is allowed, it
                              > > won't allow it. That means remove the operator ObjectType*().[/color]
                              >
                              > Well there is -at least- another way like defining the ~Object in the
                              > protected section, but doing so would prevent me to define an Object on
                              > the stack.
                              > I thought there could be another way...
                              >
                              > -M[/color]


                              But your are allowed to create an instance of smartPtr<Object > on the
                              stack.
                              I think your object should have only one lifetime philosophy --
                              reference counted with smart pointers or not.

                              Comment

                              Working...