Re: Memory leak

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 05/16/04


Date: Sun, 16 May 2004 05:44:12 +0100


"John" <johnw822003@yahoo.com> wrote in message
news:c30e885a.0405152011.6487d244@posting.google.com...
> 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



Relevant Pages

  • Problem found
    ... maybe this is because the system is out of memory and an access ... I think a try except block around a constructor is enough to prevent ... and a destructor would fail like this... ... SetLength(vChild, vChildTableSize); ...
    (alt.comp.lang.borland-delphi)
  • Re: Overloading new and delete in C++.
    ... > weeks and have read up on new and overloading it to some extent but there ... otherwise to allocate the memory for your new Foo instance, ... the constructor to initialise the returned raw memory. ... > doesn't call the constructor and free won't call the destructor.. ...
    (comp.lang.cpp)
  • Re: Memory Allocation: new int[s] vs. vector<int>p; p(s)
    ... The memory leak issue has to do with when destructors ... > get called if an exception is thrown. ... > If initthrows an exception, then it occurs before Y's constructor has ... and hence Y's destructor won't get called. ...
    (comp.lang.cpp)
  • [NT] Services for UNIX 2.0 Suffer from a Remotely Triggered Memory Leak
    ... Services for UNIX 2.0 Suffer from a Remotely Triggered Memory Leak ... 2000 are not affected by the vulnerability. ... * A vulnerability that could enable an attacker to cause the NFS service ...
    (Securiteam)
  • RE: Memory increasing even when in debug-break
    ... there are 2 types of memory leak in .Net: ... More interesting is the debugger breaks while memory still increasing ... Microsoft Online Community Support ...
    (microsoft.public.vsnet.debugging)