Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

memory leak

2 views
Skip to first unread message

Simon Guertin

unread,
Dec 2, 2005, 2:09:48 PM12/2/05
to
Hi, I am doing this and it leaks! This bit of code gets called every
second by a timer. If I just allocate and deallocate in the same pulse
It does not leak.
Any idea why?

thanks

if ( a == NULL )
{
int aSize = rand() + 1;
int bSize = rand() + 1;
int cSize = rand() + 1;

a = new char[aSize+1];
b = new char[bSize+1];
c = new char[cSize+1];

FillMemory( a, aSize, 'a' );
FillMemory( b, bSize, 'b' );
FillMemory( c, cSize, 'c' );

a[aSize] = '\0';
b[bSize] = '\0';
c[cSize] = '\0';
}
else
{
delete[] a;
delete[] b;
delete[] c;

a = NULL;
b = NULL;
c = NULL;
}

David Erbas-White

unread,
Dec 2, 2005, 2:20:16 PM12/2/05
to
Just a thought, but perhaps the code is being called somewhat
recursively, since the timer might be running in a separate thread. I
would use some semaphore flags to ensure that the timer code doesn't get
called while it's being processed. Or you could disable/enable the
timer at the beginning/end of the allocation/deallocation code.

David Erbas-White

Jonathan Benedicto

unread,
Dec 2, 2005, 2:28:24 PM12/2/05
to

A TTimer does not involve threads at all.

Jonathan


Remy Lebeau (TeamB)

unread,
Dec 2, 2005, 2:54:33 PM12/2/05
to

"Simon Guertin" <sguer...@trellianetworks.com> wrote in message
news:4390...@newsgroups.borland.com...

> Hi, I am doing this and it leaks!

How do you know that it leaks?

> This bit of code gets called every second by a timer.

Are you getting an even number of timer elapses before you stop the timer?
If the timer elapses an odd number of times, the delete statements won't be
executed the final time, thus leaking memory.


Gambit


Simon Guertin

unread,
Dec 2, 2005, 3:01:35 PM12/2/05
to
We think it leaks at 75%
Our applications has many timers and I made a seperate project with this
window that does not leak outside my application but when I bring this
window in my application it leeks. I don't understand why or where.
Maybee it has to do with a timer at 100 ms and another one ay 1000 ms.
maybee the code in the 100 ms has not completely finished processing and
another 100 ms pulse has come in..

anyway, I was just investigating this lead.

Remy Lebeau (TeamB)

unread,
Dec 2, 2005, 5:59:14 PM12/2/05
to

"Simon Guertin" <sguer...@trellianetworks.com> wrote in message
news:4390a859$1...@newsgroups.borland.com...

> We think it leaks at 75%

But you haven't explained WHY you think that. What exactly makes you think
that a leak is occuring at all?

> Our applications has many timers and I made a seperate project
> with this window that does not leak outside my application but
> when I bring this window in my application it leeks.

What exactly does the window do? You need to provide more details.

> I don't understand why or where.

Neither can anyone else until you explain exactly what you are doing with
it.

> Maybee it has to do with a timer at 100 ms and another one ay 1000 ms.

That in itself will not cause leaks.

> maybee the code in the 100 ms has not completely finished
> processing and another 100 ms pulse has come in..

Even if it did, the second timer will not actually be triggered until the
first timer finishes processing the event that is already in progress.
Unless you are using threaded timers, then your timers are serialized.
Unless you are using a message-based timer (such as TTimer) and the second
timer is reentrant because the first timer processing is pumping the message
queue for new messages. Again, it is very hard to diagnose our problem
because you have not provided any details about what you are actually doing
in your code.


Gambit


0 new messages