memory leakage in c++

A

andylcx

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!
 
P

Pavel Koryakin

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

Pavel Koryakin

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

andylcx

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

red floyd

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

Dan Cernat

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
 
M

msalters

(e-mail address removed) 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
 
T

Torsten Mueller

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,479
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top