Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Memory leak

Reply
Thread Tools

Memory leak

 
 
John
Guest
Posts: n/a
 
      05-16-2004
Hi all:

When I run my code, I find that the memory that the code uses keeps
increasing.
I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
memory by the time it finishes execution. But I do not think it needs
so much memory. About 500M memory should be enough. I have following
questions about memory leak.
(1).If in my code I only define constructor for my class, and do not
define destructor, will it cause memory leak?
(2).If in my code I only use "new" to declare new object, and do not
use "delete", will it cause memory leak?

For example, in the following code:

void class1::function1()
{
class2 *r1;
class2 *r2 = new class2;
class2 *rr[20];

......

function2(rr);

......
//r1 and r2 are also used in function1().

}

In the above code, I have two classes and I define constructor for the
two classes and do not define destructor. In function1(), I declare
two pointers of class2 and an array of pointer of class2. The array rr
is used to bring back values from function2(). For r2, I do not use
"delete".
Will r1, r2 and the array rr[] cause memory leak?
The two pointers, r1 and r2, and array rr[] are local variables, when
the code exits function1(), these local variables should be released
automatically.
Am I right?

Thanks a lot.

John
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-16-2004

"John" <> wrote in message
news: om...
> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following
> questions about memory leak.
> (1).If in my code I only define constructor for my class, and do not
> define destructor, will it cause memory leak?


Depends on the class.

> (2).If in my code I only use "new" to declare new object, and do not
> use "delete", will it cause memory leak?


Yes.

Its a very simple rule, nothing to do with constructors or destructors. When
your program runs every new allocates some memory, if you don't do a delete
for the same memory then you have a memory leak.


>
> For example, in the following code:
>
> void class1::function1()
> {
> class2 *r1;
> class2 *r2 = new class2;
> class2 *rr[20];
>
> ......
>
> function2(rr);
>
> ......
> //r1 and r2 are also used in function1().
>
> }
>
> In the above code, I have two classes and I define constructor for the
> two classes and do not define destructor.


That's irrelevant.

> In function1(), I declare
> two pointers of class2 and an array of pointer of class2. The array rr
> is used to bring back values from function2(). For r2, I do not use
> "delete".
> Will r1, r2 and the array rr[] cause memory leak?


Where are the deletes? There are no deletes so there are memory leaks all
over the place.

> The two pointers, r1 and r2, and array rr[] are local variables, when
> the code exits function1(), these local variables should be released
> automatically.
> Am I right?


Wrong. The variables destructed and the memory they occupy is 'released',
BUT the memory they might be pointing to is not released.

void f()
{
X x;
X* xp = new X();
...
}

When you get to the end of this function, the memory for x and xp are both
released. That has nothing to do with the memory pointed to by xp, which is
a completely different thing. The memory for a pointer and the memory that
it points to are not the same thing.

It's very simple, every new must be matched by a delete.

john


 
Reply With Quote
 
 
 
 
Kutty Banerjee
Guest
Posts: n/a
 
      05-16-2004

"John" <> wrote in message
news: om...
> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following
> questions about memory leak.
> (1).If in my code I only define constructor for my class, and do not
> define destructor, will it cause memory leak?
> (2).If in my code I only use "new" to declare new object, and do not
> use "delete", will it cause memory leak?
>
> For example, in the following code:
>
> void class1::function1()
> {
> class2 *r1;
> class2 *r2 = new class2;
> class2 *rr[20];
>
> ......
>
> function2(rr);
>
> ......
> //r1 and r2 are also used in function1().
>
> }
>
> In the above code, I have two classes and I define constructor for the
> two classes and do not define destructor. In function1(), I declare
> two pointers of class2 and an array of pointer of class2. The array rr
> is used to bring back values from function2(). For r2, I do not use
> "delete".
> Will r1, r2 and the array rr[] cause memory leak?
> The two pointers, r1 and r2, and array rr[] are local variables, when
> the code exits function1(), these local variables should be released
> automatically.
> Am I right?
>
> Thanks a lot.
>
> John

Hi,
check out "auto_ptr" from any standard c++ boooks.

kutty


 
Reply With Quote
 
Ian
Guest
Posts: n/a
 
      05-16-2004
John wrote:
> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following
> questions about memory leak.
> (1).If in my code I only define constructor for my class, and do not
> define destructor, will it cause memory leak?


See next answer!
> (2).If in my code I only use "new" to declare new object, and do not
> use "delete", will it cause memory leak?
>

Yes, without a destructor, how is the memory you allocated in the
destructor freed?

An alternative is to use std::auto_ptr rather than a plain pointer.

As a rule, any class with pointers must have a destructor and copy
constructor (if nothing else, this makes you think about ownership of
the data you reference through a pointer).

Ian

> For example, in the following code:
>
> void class1::function1()
> {
> class2 *r1;
> class2 *r2 = new class2;
> class2 *rr[20];
>
> ......
>
> function2(rr);
>
> ......
> //r1 and r2 are also used in function1().
>
> }
>
> In the above code, I have two classes and I define constructor for the
> two classes and do not define destructor. In function1(), I declare
> two pointers of class2 and an array of pointer of class2. The array rr
> is used to bring back values from function2(). For r2, I do not use
> "delete".
> Will r1, r2 and the array rr[] cause memory leak?
> The two pointers, r1 and r2, and array rr[] are local variables, when
> the code exits function1(), these local variables should be released
> automatically.
> Am I right?
>
> Thanks a lot.
>
> John

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      05-16-2004

"Ian" <> wrote in message
news:...
> John wrote:
> > Hi all:
> >
> > When I run my code, I find that the memory that the code uses keeps
> > increasing.
> > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> > memory by the time it finishes execution. But I do not think it needs
> > so much memory. About 500M memory should be enough. I have following
> > questions about memory leak.
> > (1).If in my code I only define constructor for my class, and do not
> > define destructor, will it cause memory leak?

>
> See next answer!
> > (2).If in my code I only use "new" to declare new object, and do not
> > use "delete", will it cause memory leak?
> >

> Yes, without a destructor, how is the memory you allocated in the
> destructor freed?
>
> An alternative is to use std::auto_ptr rather than a plain pointer.
>
> As a rule, any class with pointers must have a destructor and copy
> constructor (if nothing else, this makes you think about ownership of
> the data you reference through a pointer).
>
> Ian
>


I think the OP is not talking about a class with pointers but a pointer to a
class. Certainly that is what his code shows.

In any case the rule is every new must be matched with a delete.
Constructors and destructors are just a useful way of making sure that this
rule is followed.

Since the OP is clearly struggling with pointers I would advise avoid using
new where possible. It's certainly a common newbie trait to use new where it
isn't necessary.

// newbie style coding
void func()
{
X* xp = new X;
x->some_func();
delete x;
}

// simpler, safer and better coding
void func()
{
X x;
x.some_func();
}

There's nothing in the code that John posted that indicated he must use new
at all.

john


 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-16-2004
John wrote:

> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following
> questions about memory leak.
> (1).If in my code I only define constructor for my class, and do not
> define destructor, will it cause memory leak?


That depends on what the class does. If it allocates any resources, it
should have a destructor that deallocates them.

> (2).If in my code I only use "new" to declare new object, and do not
> use "delete", will it cause memory leak?


Yes. Everything you got from new should be deleted as soon as you don't
need it anymore.

> For example, in the following code:
>
> void class1::function1()
> {
> class2 *r1;
> class2 *r2 = new class2;
> class2 *rr[20];
>
> ......
>
> function2(rr);
>
> ......
> //r1 and r2 are also used in function1().
>
> }
>
> In the above code, I have two classes and I define constructor for the
> two classes and do not define destructor. In function1(), I declare
> two pointers of class2 and an array of pointer of class2. The array rr
> is used to bring back values from function2(). For r2, I do not use
> "delete".
> Will r1, r2 and the array rr[] cause memory leak?


If you don't delete them, yes. However, since the pointers are local
variables within your function and not member variables of your class,
you don't need a destructor for cleaning them up, but rather would
delete them in the function. Do you actually need them to be pointers
anyway? If a direct instance suffices, use it.

> The two pointers, r1 and r2, and array rr[] are local variables, when
> the code exits function1(), these local variables should be released
> automatically.


The local variables are, but not the objects they point to.

> Am I right?


What you wrote is right, but I don't think it's what you meant

 
Reply With Quote
 
Anil Mamede
Guest
Posts: n/a
 
      05-16-2004
John wrote:

> Hi all:
>
> When I run my code, I find that the memory that the code uses keeps
> increasing.
> I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> memory by the time it finishes execution. But I do not think it needs
> so much memory. About 500M memory should be enough. I have following


Maybe you're creating objects inside a infinit while or for loop.

Anil Mamede

 
Reply With Quote
 
JKop
Guest
Posts: n/a
 
      05-16-2004

Where you use "new", you must use "delete".

Where you use "new", you must use "delete".

Where you use "new", you must use "delete".


class2* r2 = new class2;

delete r2;



I highly suggest that you keep the asterisk beside THE CLASS NAME rather
than beside the variable name.

Think of "delete" as a function that takes one paramater, a pointer. You
pass it the value of the variable "r2", which is of type "class2*".

delete r2;


---


As for normal run-of-the-mill variables, they come into being at the
beginning of the block of code and are destroyed at the end of the block of
code:


int main(void)
{ //Right here, a comes into being

class2 a;

{ //Right here, b comes into being

class2 b;

} // Right here, b is destroyed.


return 0;

} //Right here, a is destroyed

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      05-16-2004
JKop wrote:

> Think of "delete" as a function that takes one paramater, a pointer.
> You pass it the value of the variable "r2", which is of type
> "class2*".
>
> delete r2;


I'd rather say think of new as a source and delete as a sink. Everything
that came from 'new' has to go to 'delete'.

> As for normal run-of-the-mill variables, they come into being at the
> beginning of the block


No, they start existing where they are defined.

> of code and are destroyed at the end of the
> block of code:
>
>
> int main(void)
> { //Right here, a comes into being


Nope. a doesn't exist yet.

> class2 a;


Here, a commes into existance.

>
> { //Right here, b comes into being


No. Same as a

>
> class2 b;
> } // Right here, b is destroyed.
>
>
> return 0;
>
> } //Right here, a is destroyed


 
Reply With Quote
 
John
Guest
Posts: n/a
 
      05-16-2004
"John Harrison" <> wrote in message news:<>...
> "Ian" <> wrote in message
> news:...
> > John wrote:
> > > Hi all:
> > >
> > > When I run my code, I find that the memory that the code uses keeps
> > > increasing.
> > > I have a PC with 2G RAM running Debian linux. The code consumes 1.5G
> > > memory by the time it finishes execution. But I do not think it needs
> > > so much memory. About 500M memory should be enough. I have following
> > > questions about memory leak.
> > > (1).If in my code I only define constructor for my class, and do not
> > > define destructor, will it cause memory leak?

> >
> > See next answer!
> > > (2).If in my code I only use "new" to declare new object, and do not
> > > use "delete", will it cause memory leak?
> > >

> > Yes, without a destructor, how is the memory you allocated in the
> > destructor freed?
> >
> > An alternative is to use std::auto_ptr rather than a plain pointer.
> >
> > As a rule, any class with pointers must have a destructor and copy
> > constructor (if nothing else, this makes you think about ownership of
> > the data you reference through a pointer).
> >
> > Ian
> >

>
> I think the OP is not talking about a class with pointers but a pointer to a
> class. Certainly that is what his code shows.
>
> In any case the rule is every new must be matched with a delete.
> Constructors and destructors are just a useful way of making sure that this
> rule is followed.
>
> Since the OP is clearly struggling with pointers I would advise avoid using
> new where possible. It's certainly a common newbie trait to use new where it
> isn't necessary.
>
> // newbie style coding
> void func()
> {
> X* xp = new X;
> x->some_func();
> delete x;
> }
>
> // simpler, safer and better coding
> void func()
> {
> X x;
> x.some_func();
> }
>
> There's nothing in the code that John posted that indicated he must use new
> at all.
>
> john


Hi John,

Thanks a lot.
My code is based on an old code. I modify it and add my own functions.
The old code uses linked list of objects everywhere. The linked list
is implemented by using pointer. This is the reason why I must use
pointer to a class.
If I do not use "new", will it cause memory leak?
For example,

void func()
{
int *p = 0; // Initialize p to be 0.
int a = 10;
X *x; //line 1
x->some_func();
p = & a;
}

Will the above code cause memory leak?
When line 1 is executed, the constructor of X will initialize x. By
the end of func(), x is released. Am I right?

Thanks again.

John
 
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 leak even after deleting memory pointers from vector cham C++ 5 09-25-2008 10:30 AM
Leak or no leak ?? Richard Heathfield C Programming 4 07-10-2006 11:37 AM
Dynamic memory allocation and memory leak... s.subbarayan C Programming 10 03-22-2005 02:48 PM
Memory leak??? (top reporting high memory usage under Solaris) Mark Probert Ruby 4 02-09-2005 06:13 PM
Wireless Zero Configuration Memory Leak?? =?Utf-8?B?Umlja3NjaHVsdHox?= Wireless Networking 3 01-19-2005 11:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57