Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > memory leakage in c++

Reply
Thread Tools

memory leakage in c++

 
 
andylcx@hotmail.com
Guest
Posts: n/a
 
      06-23-2005
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!

 
Reply With Quote
 
 
 
 
Pavel Koryakin
Guest
Posts: n/a
 
      06-23-2005
>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.

 
Reply With Quote
 
 
 
 
Pavel Koryakin
Guest
Posts: n/a
 
      06-23-2005
>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.

 
Reply With Quote
 
andylcx@hotmail.com
Guest
Posts: n/a
 
      06-23-2005
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
...
}

 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      06-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
Dan Cernat
Guest
Posts: n/a
 
      06-23-2005


(E-Mail Removed) 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

 
Reply With Quote
 
msalters
Guest
Posts: n/a
 
      06-24-2005


(E-Mail 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

 
Reply With Quote
 
Torsten Mueller
Guest
Posts: n/a
 
      06-24-2005
(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Memory Leakage drawing Images from Stream - Minimum example E. Naubauer Java 6 02-24-2006 08:57 AM
How to identify the memory leakage... John Hilton ASP .Net 0 12-16-2004 04:50 PM
Memory leakage problem with a database application Devian Java 7 09-20-2004 05:13 AM
C++ / JNI memory leakage, help needed Sambucus Java 19 05-06-2004 08:58 AM
w3wp crash and memory leakage GaŽl ASP .Net 1 10-16-2003 01:43 AM



Advertisments