C++: destructor and delete

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

    C++: destructor and delete

    Hi,

    Statement 1: "A dynamically created local object will call it's destructor
    method when it goes out of scope when a procedure returms"

    Agree.


    Statement 2: "A dynamically created object will call it's destructor when it
    is made a target of a delete".

    Does not make sense to me. Would not this result in an endless loop? (ie.
    delete calls destructor and within destructor it calls delete, which in turn
    calls the destructor again and so on....)

    Any comments appreciated.

    asasas



  • Andrew Ward

    #2
    Re: destructor and delete

    Where are these statements from?
    You have to differentiate between objects created dynamically on the heap
    with the 'new' operator, and objects created on the stack in a scope.
    Using the delete operator instructs the program to call the destructor of
    the class and then free the memory for the object. Objects that simply go
    out of scope implicitely have the same process performed on them.


    "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
    news:3f532593_1 @news.iprimus.c om.au...[color=blue]
    > Hi,
    >
    > Statement 1: "A dynamically created local object will call it's destructor
    > method when it goes out of scope when a procedure returms"
    >
    > Agree.
    >
    >
    > Statement 2: "A dynamically created object will call it's destructor when[/color]
    it[color=blue]
    > is made a target of a delete".
    >
    > Does not make sense to me. Would not this result in an endless loop? (ie.
    > delete calls destructor and within destructor it calls delete, which in[/color]
    turn[color=blue]
    > calls the destructor again and so on....)
    >
    > Any comments appreciated.
    >
    > asasas
    >
    >
    >[/color]


    Comment

    • John Harrison

      #3
      Re: destructor and delete


      "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
      news:3f532593_1 @news.iprimus.c om.au...[color=blue]
      > Hi,
      >
      > Statement 1: "A dynamically created local object will call it's destructor
      > method when it goes out of scope when a procedure returms"
      >
      > Agree.
      >
      >
      > Statement 2: "A dynamically created object will call it's destructor when[/color]
      it[color=blue]
      > is made a target of a delete".
      >
      > Does not make sense to me. Would not this result in an endless loop? (ie.
      > delete calls destructor and within destructor it calls delete, which in[/color]
      turn[color=blue]
      > calls the destructor again and so on....)
      >
      > Any comments appreciated.
      >
      > asasas
      >[/color]

      Destructor does not call delete, so there is no loop.

      john


      Comment

      • Josephine Schafer

        #4
        Re: destructor and delete


        "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
        news:3f532593_1 @news.iprimus.c om.au...[color=blue]
        > Hi,
        >
        > Statement 1: "A dynamically created local object will call it's destructor
        > method when it goes out of scope when a procedure returms"
        >
        > Agree.[/color]

        Consider this code -

        #include <iostream>
        class A{
        public:
        A()
        {
        }
        ~A()
        {
        std::cout << "Inside D'tor";
        }
        };

        void f()
        {
        A a;
        A *ptrA = new A;
        }

        int main ()
        {
        f ();
        }

        If you mean that when f() returns two destructors will be called then it's
        wrong.
        Destructor would be called only for the object created on the heap.
        In fact this is a memory leak.

        [color=blue]
        > Statement 2: "A dynamically created object will call it's destructor when[/color]
        it[color=blue]
        > is made a target of a delete".
        >[/color]

        That's true.

        [color=blue]
        > Does not make sense to me. Would not this result in an endless loop? (ie.
        > delete calls destructor and within destructor it calls delete, which in[/color]
        turn[color=blue]
        > calls the destructor again and so on....)
        >[/color]
        Destructor does not call delete unless in your destructor you write -
        delete this;

        HTH,
        J.Schafer


        Comment

        • Josephine Schafer

          #5
          Re: destructor and delete

          [color=blue]
          > Destructor would be called only for the object created on the heap.[/color]

          I meant stack ofcourse ;-).


          Comment

          • Newsnet Customer

            #6
            Re: destructor and delete

            > "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message[color=blue]
            > news:3f532593_1 @news.iprimus.c om.au...[color=green]
            > > Hi,
            > >
            > > Statement 1: "A dynamically created local object will call it's[/color][/color]
            destructor[color=blue][color=green]
            > > method when it goes out of scope when a procedure returms"
            > >
            > > Agree.
            > >
            > >
            > > Statement 2: "A dynamically created object will call it's destructor[/color][/color]
            when[color=blue]
            > it[color=green]
            > > is made a target of a delete".
            > >
            > > Does not make sense to me. Would not this result in an endless loop?[/color][/color]
            (ie.[color=blue][color=green]
            > > delete calls destructor and within destructor it calls delete, which in[/color]
            > turn[color=green]
            > > calls the destructor again and so on....)
            > >
            > > Any comments appreciated.
            > >
            > > asasas
            > >[/color]
            >
            > Destructor does not call delete, so there is no loop.
            >
            > john[/color]

            Generally speaking, you have a delete in a destructor method. That said, a
            delete calls the destructor and the endless cycles continues. I'm sure there
            is something im missing.


            sasas


            Comment

            • Newsnet Customer

              #7
              Re: destructor and delete


              [color=blue]
              > Where are these statements from?
              > You have to differentiate between objects created dynamically on the heap
              > with the 'new' operator, and objects created on the stack in a scope.
              > Using the delete operator instructs the program to call the destructor of
              > the class and then free the memory for the object.[/color]

              and whats USUALLY in the destructor method? a delete statement also,and
              hence the loop.


              sasasasas


              Comment

              • Peter van Merkerk

                #8
                Re: destructor and delete

                > > Destructor does not call delete, so there is no loop.[color=blue][color=green]
                > >[/color]
                > Generally speaking, you have a delete in a destructor method. That[/color]
                said, a[color=blue]
                > delete calls the destructor and the endless cycles continues. I'm sure[/color]
                there[color=blue]
                > is something im missing.[/color]

                A destructor never calls the delete operator on itself (this), hence no
                endless loop. In a destructor the delete operator (if any) is only used
                for other objects owned by that class instance, not for itself.

                --
                Peter van Merkerk
                peter.van.merke rk(at)dse.nl




                Comment

                • Scott Condit

                  #9
                  Re: destructor and delete

                  Newsnet Customer wrote:[color=blue][color=green]
                  >>Where are these statements from?
                  >>You have to differentiate between objects created dynamically on the heap
                  >>with the 'new' operator, and objects created on the stack in a scope.
                  >>Using the delete operator instructs the program to call the destructor of
                  >>the class and then free the memory for the object.[/color]
                  >
                  >
                  > and whats USUALLY in the destructor method? a delete statement also,and
                  > hence the loop.[/color]

                  What loop?

                  delete statements can often be found in destructors, but they
                  do not target the object whose destructor it is.

                  S

                  Comment

                  • Newsnet Customer

                    #10
                    Re: destructor and delete



                    [color=blue][color=green]
                    > > Destructor would be called only for the object created on the heap.[/color]
                    >
                    > I meant stack ofcourse ;-).[/color]

                    incorrect. as indicated in statement 1. an object's (dynamically created
                    ....meaning HEAP) destructor is called when the object goes out of scope.


                    Comment

                    • Josephine Schafer

                      #11
                      Re: destructor and delete


                      "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
                      news:3f534b75_1 @news.iprimus.c om.au...[color=blue]
                      >
                      >
                      >[color=green][color=darkred]
                      > > > Destructor would be called only for the object created on the heap.[/color]
                      > >
                      > > I meant stack ofcourse ;-).[/color]
                      >
                      > incorrect. as indicated in statement 1. an object's (dynamically created
                      > ...meaning HEAP) destructor is called when the object goes out of scope.[/color]

                      Why don't you run the code I posted and see for yourself if the destructor
                      gets called.
                      --
                      JS


                      Comment

                      • Karl Heinz Buchegger

                        #12
                        Re: C++: destructor and delete



                        Newsnet Customer wrote:[color=blue]
                        >
                        > Hi,
                        >
                        > Statement 1: "A dynamically created local object will call it's destructor
                        > method when it goes out of scope when a procedure returms"
                        >[/color]

                        A dynamically created local object ...

                        int main()
                        {
                        std::string *p;

                        p = new std::string;

                        .... calls it's destructor method when it goes out of scope

                        well. p goes out of scope. But that does not mean, that the
                        dynamically created object, where p points to is destroyed.
                        [color=blue]
                        > Agree.
                        >
                        > Statement 2: "A dynamically created object will call it's destructor when it
                        > is made a target of a delete".[/color]

                        p = new std::string;

                        ...

                        delete p;

                        the string which is pointed to by p is destroyed.
                        [color=blue]
                        >
                        > Does not make sense to me. Would not this result in an endless loop? (ie.
                        > delete calls destructor and within destructor it calls delete, which in turn
                        > calls the destructor again and so on....)[/color]

                        Why should this happen?

                        --
                        Karl Heinz Buchegger
                        kbuchegg@gascad .at

                        Comment

                        • John Harrison

                          #13
                          Re: destructor and delete


                          "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
                          news:3f533d83_1 @news.iprimus.c om.au...[color=blue][color=green]
                          > > "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
                          > > news:3f532593_1 @news.iprimus.c om.au...[color=darkred]
                          > > > Hi,
                          > > >
                          > > > Statement 1: "A dynamically created local object will call it's[/color][/color]
                          > destructor[color=green][color=darkred]
                          > > > method when it goes out of scope when a procedure returms"
                          > > >
                          > > > Agree.
                          > > >
                          > > >
                          > > > Statement 2: "A dynamically created object will call it's destructor[/color][/color]
                          > when[color=green]
                          > > it[color=darkred]
                          > > > is made a target of a delete".
                          > > >
                          > > > Does not make sense to me. Would not this result in an endless loop?[/color][/color]
                          > (ie.[color=green][color=darkred]
                          > > > delete calls destructor and within destructor it calls delete, which[/color][/color][/color]
                          in[color=blue][color=green]
                          > > turn[color=darkred]
                          > > > calls the destructor again and so on....)
                          > > >
                          > > > Any comments appreciated.
                          > > >
                          > > > asasas
                          > > >[/color]
                          > >
                          > > Destructor does not call delete, so there is no loop.
                          > >
                          > > john[/color]
                          >
                          > Generally speaking, you have a delete in a destructor method. That said, a
                          > delete calls the destructor and the endless cycles continues. I'm sure[/color]
                          there[color=blue]
                          > is something im missing.
                          >
                          >[/color]

                          Sometimes you delete a subobject in the destructor. You don't delete the
                          original object.

                          class X
                          {
                          ~X()
                          {
                          delete ptr; // delete Y object, calls Y destructor
                          }
                          Y* ptr;
                          };

                          X* p = new X;
                          delete p; // deletes an X object, calls X destructor

                          The only way there would be a loop was if you did 'delete this' in the
                          destructor.

                          john


                          Comment

                          • John Harrison

                            #14
                            Re: destructor and delete


                            "Newsnet Customer" <nistygravy@ipr imus.com.au> wrote in message
                            news:3f534b75_1 @news.iprimus.c om.au...[color=blue]
                            >
                            >
                            >[color=green][color=darkred]
                            > > > Destructor would be called only for the object created on the heap.[/color]
                            > >
                            > > I meant stack ofcourse ;-).[/color]
                            >
                            > incorrect. as indicated in statement 1. an object's (dynamically created
                            > ...meaning HEAP) destructor is called when the object goes out of scope.
                            >[/color]

                            No it is not.

                            {
                            X* ptr = new X;
                            }

                            No destructors are called in the above code, because POINTERS do not have
                            destructors. ptr is a POINTER to X, its is not an X.

                            {
                            X x;
                            }

                            X destructor is called (assuming it has one).

                            You're very cocky considering, by your own admission, you don't get it.

                            john


                            Comment

                            • Karl Heinz Buchegger

                              #15
                              Re: destructor and delete



                              Newsnet Customer wrote:[color=blue]
                              >[color=green][color=darkred]
                              > > > Destructor would be called only for the object created on the heap.[/color]
                              > >
                              > > I meant stack ofcourse ;-).[/color]
                              >
                              > incorrect. as indicated in statement 1. an object's (dynamically created
                              > ...meaning HEAP) destructor is called when the object goes out of scope.[/color]

                              The point is: a dynamically created object(that means on the heap) *never*
                              goes out of scope. The object is destroyed when you explicitely say so
                              with the help of delete.

                              {
                              std::string* p;
                              p = new std::string;
                              }

                              At this point, p goes out of scope and hence is destroyed.
                              But the dynamically allocated string object lives on, even
                              if there is no means to access it any longer.

                              --
                              Karl Heinz Buchegger
                              kbuchegg@gascad .at

                              Comment

                              Working...