memory leakage in c++

Discussion in 'C++' started by andylcx@hotmail.com, Jun 23, 2005.

  1. Guest

    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(acctually, 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!
     
    , Jun 23, 2005
    #1
    1. Advertising

  2. >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.
     
    Pavel Koryakin, Jun 23, 2005
    #2
    1. Advertising

  3. >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.
     
    Pavel Koryakin, Jun 23, 2005
    #3
  4. Guest

    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
    ...
    }
     
    , Jun 23, 2005
    #4
  5. red floyd Guest

    wrote:
    > 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


    Don't worry about "efficient" until you have code that works properly.
    Hoare's Law: "Premature optimization is the root of all evil".

    > 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
    > ...
    > }
    >


    Standard idiom. If an object owns a pointer, it deletes it upon
    destruction.
     
    red floyd, Jun 23, 2005
    #5
  6. Dan Cernat Guest

    wrote:
    > 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
    > ...
    > }


    *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
     
    Dan Cernat, Jun 23, 2005
    #6
  7. msalters Guest

    schreef:
    > what I did it now is like below:
    > in the .h file, I define a pointer point to class B;


    You should put it in the class declaration.

    > 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
    > ...
    > }


    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
     
    msalters, Jun 24, 2005
    #7
  8. schrieb:

    > 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;


    At first, make the pointer a member variable of the class. What about
    initialization? Initialize it by NULL;

    > A.cpp file:
    > A::~A()
    > {
    > delete pr;
    > }
    >
    > void A::function()
    > {
    > B *newobject = new B;


    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.
     
    Torsten Mueller, Jun 24, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gaël

    w3wp crash and memory leakage

    Gaël, Oct 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    2,701
    Alvin Bruney
    Oct 16, 2003
  2. John Hilton

    How to identify the memory leakage...

    John Hilton, Dec 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    318
    John Hilton
    Dec 16, 2004
  3. Sambucus
    Replies:
    19
    Views:
    889
    George Neuner
    May 6, 2004
  4. Devian
    Replies:
    7
    Views:
    387
    Babu Kalakrishnan
    Sep 20, 2004
  5. E. Naubauer
    Replies:
    6
    Views:
    3,023
    Roedy Green
    Feb 24, 2006
Loading...

Share This Page