memory leakage in c++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • andylcx@hotmail.com

    memory leakage in c++

    hi all:
    I have a code like below, is there any serious memory leakage in my
    code. I am confusion now but no idea how to fix it. In the member
    function of class A, I create a new object of class B like below:

    void A::function()
    {
    B *newobject = new B;
    newobject->....;
    newobject->....; //did some action here
    ...

    }
    But I did not delete newobject in the A::function(acc tually, I cannot
    delete it since I need to use it later. Will the newobject be
    automatically destroy when the main function end? Is the memory leakage
    problem serious in this case? Thanks a lot!

  • Pavel Koryakin

    #2
    Re: memory leakage in c++

    >I cannot delete it since I need to use it later
    Where do you use it ? From this example I can see that you can use it
    only within A::function() .

    You can make "newobject" a member of your class and delete it in
    destructor.

    Comment

    • Pavel Koryakin

      #3
      Re: memory leakage in c++

      >I cannot delete it since I need to use it later
      Where do you use it ? From this example I can see what you can use
      newobject only within A::function().

      Make newobject member of your class and delete it in destructor.

      Comment

      • andylcx@hotmail.com

        #4
        Re: memory leakage in c++

        what I did it now is like below:
        in the .h file, I define a pointer point to class B;
        and in the function, I assign the newobject to it.
        and in the destructor, delete this pointer. Is this method safe and
        efficient? Thanks
        this is my code:
        A.h file

        class B;
        B *pr;

        A.cpp file:
        A::~A()
        {
        delete pr;
        }

        void A::function()
        {
        B *newobject = new B;
        pr=newobjet;
        pr->....;
        pr->....; //did some action here
        ...
        }

        Comment

        • red floyd

          #5
          Re: memory leakage in c++

          andylcx@hotmail .com wrote:[color=blue]
          > what I did it now is like below:
          > in the .h file, I define a pointer point to class B;
          > and in the function, I assign the newobject to it.
          > and in the destructor, delete this pointer. Is this method safe and
          > efficient? Thanks[/color]

          Don't worry about "efficient" until you have code that works properly.
          Hoare's Law: "Premature optimization is the root of all evil".
          [color=blue]
          > this is my code:
          > A.h file
          >
          > class B;
          > B *pr;
          >
          > A.cpp file:
          > A::~A()
          > {
          > delete pr;
          > }
          >
          > void A::function()
          > {
          > B *newobject = new B;
          > pr=newobjet;
          > pr->....;
          > pr->....; //did some action here
          > ...
          > }
          >[/color]

          Standard idiom. If an object owns a pointer, it deletes it upon
          destruction.

          Comment

          • Dan Cernat

            #6
            Re: memory leakage in c++



            andylcx@hotmail .com wrote:[color=blue]
            > what I did it now is like below:
            > in the .h file, I define a pointer point to class B;
            > and in the function, I assign the newobject to it.
            > and in the destructor, delete this pointer. Is this method safe and
            > efficient? Thanks
            > this is my code:
            > A.h file
            >
            > class B;
            > B *pr;
            >
            > A.cpp file:
            > A::~A()
            > {
            > delete pr;
            > }
            >
            > void A::function()
            > {
            > B *newobject = new B;
            > pr=newobjet;
            > pr->....;
            > pr->....; //did some action here
            > ...
            > }[/color]

            *pr is a global pointer, yet it has the lifetime of an A object,
            specifficaly the lifetime of the FIRST destructed A object.
            As other pointed, why isn't pr a member of A?

            Dan

            Comment

            • msalters

              #7
              Re: memory leakage in c++



              andylcx@hotmail .com schreef:[color=blue]
              > what I did it now is like below:
              > in the .h file, I define a pointer point to class B;[/color]

              You should put it in the class declaration.
              [color=blue]
              > and in the function, I assign the newobject to it.
              > and in the destructor, delete this pointer. Is this method safe and
              > efficient? Thanks
              > this is my code:
              > A.h file
              >
              > class B;
              > B *pr;
              >
              > A.cpp file:
              > A::~A()
              > {
              > delete pr;
              > }
              >
              > void A::function()
              > {
              > B *newobject = new B;
              > pr=newobjet;
              > pr->....;
              > pr->....; //did some action here
              > ...
              > }[/color]

              Safe: not really. If you call A::function twice for one
              A object, you have two calls to new but still only one
              delete. If you have an A object but don't call A::function,
              the delete will try to delete garbage.

              There are better solutions. The first thing you need to do is put
              pr in class A:

              class B;
              class A {
              ...
              B* pr;
              // then add the basic identity functions
              A(); // will set pr to 0
              A( A const& src ); // has to copy *(src.pr)
              A& operator=( A const& src ); // has to replace this->pr
              ~A(); // clean up pr
              };

              then you have to deal with the possibility of pr already being
              allocated:
              void A::function()
              {
              if( pr==0 ) // from A::A()
              pr = new B;
              //...
              }

              You can also use std::auto_ptr<B > pr, it saves you a bit of work.

              HTH,
              Michiel Salters

              Comment

              • Torsten Mueller

                #8
                Re: memory leakage in c++

                andylcx@hotmail .com schrieb:
                [color=blue]
                > what I did it now is like below:
                > in the .h file, I define a pointer point to class B;
                > and in the function, I assign the newobject to it.
                > and in the destructor, delete this pointer. Is this method safe and
                > efficient? Thanks
                > this is my code:[/color]
                [color=blue]
                > A.h file
                >
                > class B;
                > B *pr;[/color]

                At first, make the pointer a member variable of the class. What about
                initialization? Initialize it by NULL;
                [color=blue]
                > A.cpp file:
                > A::~A()
                > {
                > delete pr;
                > }
                >
                > void A::function()
                > {
                > B *newobject = new B;[/color]

                Test it before creation. What if the object already exists? Use the
                constructor of A to create the object once.

                Generally think about the new operator. This new creates an object on
                the heap. If you just need a private object in some methods of class A
                you can use a normal member variable (not a pointer):

                class A {
                B b; // define member variable of type B
                A ()
                : b() // construct member variable b
                {
                }
                };

                void A::function()
                {
                b. ...;
                b. ...; // do actions here with b
                ...
                }

                This member variable does not need a new and requires also no
                destructor in class A at all because the destructors of all member
                variables are called automatically. This way is also most efficient
                because no heap operation has been done.

                T.M.

                Comment

                Working...