Re: Memory leak problem
- From: Michael Mair <Michael.Mair@xxxxxxxxxxxxxxx>
- Date: Sun, 12 Mar 2006 19:35:58 +0100
Fernando Barsoba schrieb:
Hi,<snip: memwatch output>
After following the advice received in this list, I have isolated the memory leak problem I am having. I am also using MEMWATCH and I think it is working properly.
The program does some calculations and stores elements in a list. After that, a sorting algorithm is used over that list.
Two functions are called before the sorting process:
List = initVoiceChannels(List, TempEvent);
List = SetTimerT1(List, TempEvent);
If I only execute these functions, no memory leak is produced. However, if I call the sorting function *after* any of these functions, the memory leak occurs.
Another test has been to store elements in list and then sorting them. I used the following loop:
-----
typedef struct EVENT
{
double codeEvent;
char descEvent[80];
double TimeInit;
double TimeExpire;
} EVENT;
-----------
mwInit();
DLLIST *List = NULL;
EVENT *TempEvent;
TempEvent = malloc(1 * sizeof *TempEvent);
double i=0;
srand(1);
for( i = 0; i < 20000; i++ ) {
TempEvent->codeEvent = i + 1;
TempEvent->TimeExpire = rand()/1000000;
DLAddAfter(&List, 0, TempEvent, sizeof *TempEvent);
TempEvent->codeEvent);
}
List = sortElements(List);
DLDestroy(&List);
mwTerm();
exit(0);
And there was NO memory leak. So I believe the problem is not in the sorting algorithm per se.
So, going back to the code that does fail. The log from memwatch is:
And the code is the following. The list is from the book "C unleashed" from Richard Heathfield et al. chapter 11.
<snip: several hundred lines of code>
Your post including the dllist code was about nine hundred lines long;
this is a little bit much for usenet.
In addition, people have to piece together the files in question.
It is usually easier if you provide a web page where you have put
up all the files; then people may have a look at them or download
them as they like and "quote" the relevant aspects back here.
I will have a closer look at your code later on but here are some
first remarks:
- As long as your computer's memory is sufficiently large, there
is no reason to use floats instead of doubles -- with floats you
are only guaranteed to have 6 digits (i.e. you cannot resolve
milliseconds over a one hour interval) and doubles are in no way
slower on modern host PCs.
- If you post code here, indicate if you are using C89 or C99 (or
K&R C) -- your code seems to indicate C99 but could also be C89
plus compiler extensions (frowned upon here because all bets are
off and only people with your compiler and compiler version can
help you in every respect) or C++ code restricted to mostly C
features (which opens a can of worms of its own). Even if you do
use C99, be careful with line comments -- they can break which
can have unforeseen consequences.
- Your code seems to be split into "main.c" and "main.h".
Consider using another file structure, e.g. "main.c", "event.c",
"event.h" -- this makes things easier if you want to reuse the
code; you may even want to consider using "main.c", "event.c",
"event.h", "sortdllist.c", "sortdllist.h", ...
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
.
- Follow-Ups:
- Re: Memory leak problem
- From: Michael Mair
- Re: Memory leak problem
- References:
- Memory leak problem
- From: Fernando Barsoba
- Memory leak problem
- Prev by Date: Re: give me some tips
- Next by Date: Re: Multiple Assignment Evaluation Debate
- Previous by thread: Re: Memory leak problem
- Next by thread: Re: Memory leak problem
- Index(es):