deleting an object twice?

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

    deleting an object twice?

    Hi,

    Does deleting an object more than one times incur undefined behavior? I
    think it doesn't but just making sure... thanks

    Rick

  • Karl Heinz Buchegger

    #2
    Re: deleting an object twice?

    Rick wrote:[color=blue]
    >
    > Hi,
    >
    > Does deleting an object more than one times incur undefined behavior?[/color]

    The first delete renders the pointer to it invalid. Using an
    invalid pointer invokes undefined behaviour.
    [color=blue]
    > I
    > think it doesn't but just making sure... thanks[/color]

    You are wrong.


    --
    Karl Heinz Buchegger
    kbuchegg@gascad .at

    Comment

    • Ali R.

      #3
      Re: deleting an object twice?

      "Rick" <rrquick@nosp am-com> wrote in message
      news:3fcf5c0d$1 @clarion.carno. net.au...[color=blue]
      > Hi,
      >
      > Does deleting an object more than one times incur undefined behavior? I
      > think it doesn't but just making sure... thanks
      >
      > Rick
      >[/color]

      Are you setting your pointer to null after the first delete? I think you
      have deleting a null pointer mixed up with deleting a pointer twice. I am
      not sure if this is a standard behavior or not but on the few compilers that
      I have worked with delete checks for a null pointer and does nothing.


      Comment

      • Hendrik Belitz

        #4
        Re: deleting an object twice?

        Rick wrote:
        [color=blue]
        > Hi,
        >
        > Does deleting an object more than one times incur undefined behavior? I
        > think it doesn't but just making sure... thanks
        >
        > Rick[/color]

        It does. Delete renders a pointer to be invalid and so calling delete again
        will result in undefined behaviour. To circumvent this, simply set the
        pointer to NULL after deletion. Calling delete for a NULL pointer will do
        nothing.

        --
        Dipl.-Inform. Hendrik Belitz
        Central Laboratory of Electronics
        Research Center Juelich

        Comment

        • tom_usenet

          #5
          Re: deleting an object twice?

          On Fri, 05 Dec 2003 03:08:58 +1100, Rick <rrquick@nosp am-com> wrote:
          [color=blue]
          >Hi,
          >
          >Does deleting an object more than one times incur undefined behavior? I
          >think it doesn't but just making sure... thanks[/color]

          int* p = new int;
          delete p;
          delete p; //undefined behaviour

          Generally double deletes cause heap corruption and, if you're lucky, a
          crash. You can of course delete null pointers as often as you like.
          e.g.

          int* p = new int;
          delete p;
          p = 0;
          delete p; //fine

          Tom

          C++ FAQ: http://www.parashift.com/c++-faq-lite/
          C FAQ: http://www.eskimo.com/~scs/C-faq/top.html

          Comment

          • Andrew Koenig

            #6
            Re: deleting an object twice?

            > Does deleting an object more than one times incur undefined behavior? I[color=blue]
            > think it doesn't but just making sure... thanks[/color]

            How can you possibly delete an object more than once? Once you have deleted
            it, it no longer exists, so what are you deleting the second time?

            If you are thinking of something like this:

            int* p = new int;
            delete p;
            delete p;

            then after the first "delete p", p is an invalid pointer -- the only thing
            you can do with p is give it a new value.

            So there is no question of deleting "an object" twice.


            Comment

            • E. Robert Tisdale

              #7
              Re: deleting an object twice?

              Andrew Koenig wrote:
              [color=blue][color=green]
              >>Does deleting an object more than one times incur undefined behavior?
              >>I think it doesn't but just making sure... thanks[/color]
              >
              > How can you possibly delete an object more than once?
              > Once you have deleted it, it no longer exists,
              > so what are you deleting the second time?
              >
              > If you are thinking of something like this:
              >
              > int* p = new int;
              > delete p;
              > delete p;
              >
              > then after the first "delete p", p is an invalid pointer --[/color]

              I object to your abuse of the English language.
              The pointer p is *not* deleted.
              The *object* to which pointer p points is deleted.
              Pointer p is a valid pointer to an invalid object
              (of type int in this case) because delete calls
              the object's destructor to destroy the object.
              [color=blue]
              > the only thing you can do with p is give it a new value.
              >
              > So there is no question of deleting "an object" twice.[/color]

              The problem is that the second delete
              is almost certainly a programming error -- a bug.
              If you simply set the pointer p = NULL,
              the bug will go undetected.
              You can't do anything about this for built-in types
              but you can for User Defined Types (UDTs):

              class X {
              private:
              int Valid;
              // other data members
              public:
              X(void) Valid(0x5555555 5) { }
              ~X(void) {
              if (0x55555555 == Valid)
              Valid == 0xaaaaaaaa;
              else
              std::cerr << "Invalid object of type X!" << std::endl;
              }
              // other public functions and operators
              };

              Comment

              • David White

                #8
                Re: deleting an object twice?

                "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                news:3FCF9A21.7 050305@jpl.nasa .gov...[color=blue]
                > Andrew Koenig wrote:
                >[color=green][color=darkred]
                > >>Does deleting an object more than one times incur undefined behavior?
                > >>I think it doesn't but just making sure... thanks[/color]
                > >
                > > How can you possibly delete an object more than once?
                > > Once you have deleted it, it no longer exists,
                > > so what are you deleting the second time?
                > >
                > > If you are thinking of something like this:
                > >
                > > int* p = new int;
                > > delete p;
                > > delete p;
                > >
                > > then after the first "delete p", p is an invalid pointer --[/color]
                >
                > I object to your abuse of the English language.
                > The pointer p is *not* deleted.[/color]

                Who said it was?
                [color=blue]
                > The *object* to which pointer p points is deleted.[/color]

                As far as I can see, neither the OP nor AK have stated or implied that it is
                the pointer being deleted. Only the C++ source code says that :-)
                [color=blue]
                > Pointer p is a valid pointer to an invalid object
                > (of type int in this case) because delete calls
                > the object's destructor to destroy the object.
                >[color=green]
                > > the only thing you can do with p is give it a new value.
                > >
                > > So there is no question of deleting "an object" twice.[/color]
                >
                > The problem is that the second delete
                > is almost certainly a programming error -- a bug.
                > If you simply set the pointer p = NULL,
                > the bug will go undetected.[/color]

                I'd say that would fix the bug rather than hide it, albeit unsatisfactoril y.
                And in some circumstances two deletes are quite normal. An object's member
                pointer to a valid object might or might not have been deleted and assigned
                to null before the destructor is reached, so you would like the destructor
                to delete it regardless.

                DW



                Comment

                • Peter van Merkerk

                  #9
                  Re: deleting an object twice?

                  "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                  news:3FCF9A21.7 050305@jpl.nasa .gov...[color=blue]
                  > Andrew Koenig wrote:
                  >[color=green][color=darkred]
                  > >>Does deleting an object more than one times incur undefined behavior?
                  > >>I think it doesn't but just making sure... thanks[/color]
                  > >
                  > > How can you possibly delete an object more than once?
                  > > Once you have deleted it, it no longer exists,
                  > > so what are you deleting the second time?
                  > >
                  > > If you are thinking of something like this:
                  > >
                  > > int* p = new int;
                  > > delete p;
                  > > delete p;
                  > >
                  > > then after the first "delete p", p is an invalid pointer --[/color]
                  >
                  > I object to your abuse of the English language.
                  > The pointer p is *not* deleted.[/color]

                  I don't think Andrew said so; he just said: "after the first "delete p"". He
                  didn't say that the pointer was deleted.
                  [color=blue]
                  > The *object* to which pointer p points is deleted.
                  > Pointer p is a valid pointer to an invalid object
                  > (of type int in this case) because delete calls
                  > the object's destructor to destroy the object.
                  >[color=green]
                  > > the only thing you can do with p is give it a new value.
                  > >
                  > > So there is no question of deleting "an object" twice.[/color]
                  >
                  > The problem is that the second delete
                  > is almost certainly a programming error -- a bug.
                  > If you simply set the pointer p = NULL,
                  > the bug will go undetected.[/color]

                  It is not necessarilly a bug. There is a reason why you can do a delete on a
                  null pointer.
                  [color=blue]
                  > You can't do anything about this for built-in types
                  > but you can for User Defined Types (UDTs):
                  >
                  > class X {
                  > private:
                  > int Valid;
                  > // other data members
                  > public:
                  > X(void) Valid(0x5555555 5) { }
                  > ~X(void) {
                  > if (0x55555555 == Valid)
                  > Valid == 0xaaaaaaaa;
                  > else
                  > std::cerr << "Invalid object of type X!" << std::endl;
                  > }
                  > // other public functions and operators
                  > };[/color]

                  Even though this technique may work on some platforms with some compilers
                  under certain circumstances, it is not *guaranteed* to work. Once an object
                  has been deleted, the memory it occupied may have been overwritten, unmapped
                  (resulting in a page fault) or occupied by another object (possibly of the
                  same class). There is also a chance the vtable pointer of the deleted object
                  is damaged; if the pointer is used to call any virtual member function
                  (including the destructor) on the deleted object very weird things may
                  happen - good for hours of debugging fun.

                  Personnally I prefer resetting the pointer to NULL after deleting the object
                  it is pointing to. That way it easy to detect if the pointer is still
                  pointing to a valid object, and on most platforms leads to very predictable
                  behaviour when that pointer is accidentally dereferenced. Resetting the
                  pointer does not conflict with using a member variable to see if the object
                  is still valid. Even tough that technique is not 100% reliable, it increases
                  the chance that code that shouldn't work doesn't work (which is usually a
                  good thing). Member functions (not just the destructor) can use the member
                  variable with the magic number as a precondition. This technique can be
                  especially useful to detect lifetime issues i.c.w. having multiple pointers
                  to the same object.
                  --
                  Peter van Merkerk
                  peter.van.merke rk(at)dse.nl




                  Comment

                  • Andrew Koenig

                    #10
                    Re: deleting an object twice?

                    [color=blue][color=green]
                    > > How can you possibly delete an object more than once?
                    > > Once you have deleted it, it no longer exists,
                    > > so what are you deleting the second time?
                    > >
                    > > If you are thinking of something like this:
                    > >
                    > > int* p = new int;
                    > > delete p;
                    > > delete p;
                    > >
                    > > then after the first "delete p", p is an invalid pointer --[/color]
                    >
                    > I object to your abuse of the English language.[/color]

                    That's your prerogative. Mine is to say that you're mistaken :-)
                    [color=blue]
                    > The pointer p is *not* deleted.[/color]

                    I never said it was.
                    [color=blue]
                    > The *object* to which pointer p points is deleted.[/color]

                    Yes, that's what I said.
                    [color=blue]
                    > Pointer p is a valid pointer to an invalid object
                    > (of type int in this case) because delete calls
                    > the object's destructor to destroy the object.[/color]

                    Wrong. Once you have said "delete p", the value of p is now invalid. For
                    example, in

                    int* p = new int;
                    delete p;
                    int* q = p;

                    the attempt to copy p in order to initialize q yields undefined behavior, so
                    the implementation can do as it pleases.
                    [color=blue][color=green]
                    > > the only thing you can do with p is give it a new value.[/color][/color]
                    [color=blue][color=green]
                    > > So there is no question of deleting "an object" twice.[/color][/color]
                    [color=blue]
                    > The problem is that the second delete
                    > is almost certainly a programming error -- a bug.[/color]

                    Yes, indeed -- I never said otherwise.
                    [color=blue]
                    > If you simply set the pointer p = NULL,
                    > the bug will go undetected.[/color]

                    Yes indeed -- I never said otherwise.
                    [color=blue]
                    > You can't do anything about this for built-in types
                    > but you can for User Defined Types (UDTs):
                    >
                    > class X {
                    > private:
                    > int Valid;
                    > // other data members
                    > public:
                    > X(void) Valid(0x5555555 5) { }
                    > ~X(void) {
                    > if (0x55555555 == Valid)
                    > Valid == 0xaaaaaaaa;
                    > else
                    > std::cerr << "Invalid object of type X!" << std::endl;
                    > }
                    > // other public functions and operators
                    > };[/color]

                    And your point is what, exactly? Are you claiming that C++ requires some
                    particular behavior in the case of

                    X* p = new X;
                    delete p;
                    delete p;

                    It doesn't; the implementation can do as it pleases.


                    Comment

                    • E. Robert Tisdale

                      #11
                      Re: deleting an object twice?

                      Andrew Koenig wrote:
                      [color=blue][color=green][color=darkred]
                      >>>How can you possibly delete an object more than once?
                      >>>Once you have deleted it, it no longer exists,
                      >>>so what are you deleting the second time?
                      >>>
                      >>>If you are thinking of something like this:
                      >>>
                      >>> int* p = new int;
                      >>> delete p;
                      >>> delete p;
                      >>>
                      >>>then after the first "delete p", p is an invalid pointer --[/color]
                      >>
                      >>I object to your abuse of the English language.[/color]
                      >
                      > That's your prerogative. Mine is to say that you're mistaken :-)
                      >[color=green]
                      >>The pointer p is *not* deleted.[/color]
                      >
                      > I never said it was.[/color]

                      I never said that you said that it was.
                      Ali R., Hendrik Belitz and tom_usenet said that it was.
                      [color=blue][color=green]
                      >>The *object* to which pointer p points is deleted.[/color]
                      >
                      > Yes, that's what I said.
                      >[color=green]
                      >>Pointer p is a valid pointer to an invalid object
                      >>(of type int in this case) because delete calls
                      >>the object's destructor to destroy the object.[/color]
                      >
                      > Wrong. Once you have said "delete p", the value of p is now invalid.
                      > For example, in
                      >
                      > int* p = new int;
                      > delete p;
                      > int* q = p;
                      >
                      > the attempt to copy p in order to initialize q yields undefined behavior
                      > so the implementation can do as it pleases.[/color]

                      Where is it written? (Please cite and quote the relevant document.)

                      Please compile and run the above code fragment on a platform
                      where the behavior is other than expected
                      and show us the diagnostic and/or incorrect result.
                      [color=blue][color=green][color=darkred]
                      >>>the only thing you can do with p is give it a new value.[/color][/color]
                      >[color=green][color=darkred]
                      >>>So there is no question of deleting "an object" twice.[/color][/color]
                      >[color=green]
                      >>The problem is that the second delete
                      >>is almost certainly a programming error -- a bug.[/color]
                      >
                      > Yes, indeed -- I never said otherwise.[/color]

                      I never said that you said otherwise.
                      [color=blue][color=green]
                      >>If you simply set the pointer p = NULL,
                      >>the bug will go undetected.[/color]
                      >
                      > Yes indeed -- I never said otherwise.[/color]

                      I never said that you said otherwise.
                      [color=blue][color=green]
                      >>You can't do anything about this for built-in types
                      >>but you can for User Defined Types (UDTs):
                      >>
                      >> class X {
                      >> private:
                      >> int Valid;
                      >> // other data members
                      >> public:
                      >> X(void) Valid(0x5555555 5) { }
                      >> ~X(void) {
                      >> if (0x55555555 == Valid)
                      >> Valid == 0xaaaaaaaa;
                      >> else
                      >> std::cerr << "Invalid object of type X!" << std::endl;
                      >> }
                      >> // other public functions and operators
                      >> };[/color]
                      >
                      > And your point is what, exactly?
                      > Are you claiming that
                      > C++ requires some particular behavior in the case of
                      >
                      > X* p = new X;
                      > delete p;
                      > delete p;[/color]

                      I made no such claim.
                      [color=blue]
                      > It doesn't; the implementation can do as it pleases.[/color]

                      I never said differently.

                      Please don't take my remarks personally. They are directed at
                      other subscribers reading this thread and *not* just you.

                      Comment

                      • Ron Natalie

                        #12
                        Re: deleting an object twice?


                        "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message news:3FCFC944.9 0708@jpl.nasa.g ov...
                        [color=blue][color=green]
                        > > the attempt to copy p in order to initialize q yields undefined behavior
                        > > so the implementation can do as it pleases.[/color]
                        >
                        > Where is it written? (Please cite and quote the relevant document.)[/color]

                        5.3.5/4 of the C++ stanard



                        Comment

                        • Andrew Koenig

                          #13
                          Re: deleting an object twice?

                          > I never said that you said that it was.

                          You said that I was abusing the English language. The only reason I could
                          imagine that you might make such a statement was that you thought I had said
                          something incorrect or unclear. So I went over each statement I made, in
                          order to point out why I think it is correct.
                          [color=blue][color=green][color=darkred]
                          > >>Pointer p is a valid pointer to an invalid object
                          > >>(of type int in this case) because delete calls
                          > >>the object's destructor to destroy the object.[/color]
                          > >
                          > > Wrong. Once you have said "delete p", the value of p is now invalid.
                          > > For example, in
                          > >
                          > > int* p = new int;
                          > > delete p;
                          > > int* q = p;
                          > >
                          > > the attempt to copy p in order to initialize q yields undefined behavior
                          > > so the implementation can do as it pleases.[/color]
                          >
                          > Where is it written? (Please cite and quote the relevant document.)[/color]

                          C++ standard, subclause 5.3.5, paragraph 4: "Note: The value of a pointer
                          that refers to deallocated storage is indeterminate."
                          [color=blue]
                          > Please compile and run the above code fragment on a platform
                          > where the behavior is other than expected
                          > and show us the diagnostic and/or incorrect result.[/color]

                          Attempting to use the value of an indeterminate pointer yields undefined
                          behavior, so an implementation is allowed to do as it plases with the
                          example above. Therefore, there is no such thing as unexpected behavior --
                          any behavior at all is permissible.
                          [color=blue][color=green][color=darkred]
                          > >>The problem is that the second delete
                          > >>is almost certainly a programming error -- a bug.[/color]
                          > >
                          > > Yes, indeed -- I never said otherwise.[/color]
                          >
                          > I never said that you said otherwise.[/color]

                          See above.
                          [color=blue][color=green][color=darkred]
                          > >>If you simply set the pointer p = NULL,
                          > >>the bug will go undetected.[/color]
                          > >
                          > > Yes indeed -- I never said otherwise.[/color]
                          >
                          > I never said that you said otherwise.[/color]

                          See above.
                          [color=blue][color=green]
                          > > And your point is what, exactly?
                          > > Are you claiming that
                          > > C++ requires some particular behavior in the case of
                          > >
                          > > X* p = new X;
                          > > delete p;
                          > > delete p;[/color]
                          >
                          > I made no such claim.
                          >[color=green]
                          > > It doesn't; the implementation can do as it pleases.[/color]
                          >
                          > I never said differently.
                          >
                          > Please don't take my remarks personally. They are directed at
                          > other subscribers reading this thread and *not* just you.[/color]

                          Then why did you claim that I was abusing the English language?


                          Comment

                          • Jumbo

                            #14
                            Re: deleting an object twice?


                            "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                            news:3FCFC944.9 0708@jpl.nasa.g ov...[color=blue]
                            > Andrew Koenig wrote:
                            >[color=green][color=darkred]
                            > >>>How can you possibly delete an object more than once?
                            > >>>Once you have deleted it, it no longer exists,
                            > >>>so what are you deleting the second time?
                            > >>>
                            > >>>If you are thinking of something like this:
                            > >>>
                            > >>> int* p = new int;
                            > >>> delete p;
                            > >>> delete p;
                            > >>>
                            > >>>then after the first "delete p", p is an invalid pointer --
                            > >>
                            > >>I object to your abuse of the English language.[/color]
                            > >
                            > > That's your prerogative. Mine is to say that you're mistaken :-)
                            > >[color=darkred]
                            > >>The pointer p is *not* deleted.[/color]
                            > >
                            > > I never said it was.[/color]
                            >
                            > I never said that you said that it was.
                            > Ali R., Hendrik Belitz and tom_usenet said that it was.
                            >[/color]

                            But you implied it when you said:
                            [quote snippet]
                            I object to your abuse of the English language.
                            The pointer p is *not* deleted.
                            [end quote]

                            What did you mean by this if you didn't mean what we think you mean?


                            Comment

                            • Rolf Magnus

                              #15
                              Re: deleting an object twice?

                              David White wrote:
                              [color=blue]
                              > "E. Robert Tisdale" <E.Robert.Tisda le@jpl.nasa.gov > wrote in message
                              > news:3FCF9A21.7 050305@jpl.nasa .gov...[color=green]
                              >> Andrew Koenig wrote:
                              >>[color=darkred]
                              >> >>Does deleting an object more than one times incur undefined
                              >> >>behavior? I think it doesn't but just making sure... thanks
                              >> >
                              >> > How can you possibly delete an object more than once?
                              >> > Once you have deleted it, it no longer exists,
                              >> > so what are you deleting the second time?
                              >> >
                              >> > If you are thinking of something like this:
                              >> >
                              >> > int* p = new int;
                              >> > delete p;
                              >> > delete p;
                              >> >
                              >> > then after the first "delete p", p is an invalid pointer --[/color]
                              >>
                              >> I object to your abuse of the English language.
                              >> The pointer p is *not* deleted.[/color]
                              >
                              > Who said it was?
                              >[color=green]
                              >> The *object* to which pointer p points is deleted.[/color]
                              >
                              > As far as I can see, neither the OP nor AK have stated or implied that
                              > it is the pointer being deleted. Only the C++ source code says that
                              > :-)
                              >[color=green]
                              >> Pointer p is a valid pointer to an invalid object
                              >> (of type int in this case) because delete calls
                              >> the object's destructor to destroy the object.
                              >>[color=darkred]
                              >> > the only thing you can do with p is give it a new value.
                              >> >
                              >> > So there is no question of deleting "an object" twice.[/color]
                              >>
                              >> The problem is that the second delete
                              >> is almost certainly a programming error -- a bug.
                              >> If you simply set the pointer p = NULL,
                              >> the bug will go undetected.[/color]
                              >
                              > I'd say that would fix the bug rather than hide it, albeit
                              > unsatisfactoril y. And in some circumstances two deletes are quite
                              > normal.[/color]

                              I see this as a design error. Each new should have exactly one delete,
                              certainly not less, but also not more. And IMHO, a good design will
                              prevent any intended double deletions. Setting the pointer to 0 after
                              delete might be a useful thing for release versions to prevent a crash
                              due to an overlooked double deletion. But while debugging, I want to
                              find those and thus don't want to set the pointer to 0. If at all, I'd
                              set it to something that enforces a crash.
                              [color=blue]
                              > An object's member pointer to a valid object might or might
                              > not have been deleted and assigned to null before the destructor is
                              > reached, so you would like the destructor to delete it regardless.[/color]

                              Then the ownership isn't clear. A dynamically allocated object should
                              have an owner that is responsible for deleting it.

                              Comment

                              Working...