Re: Memory leak problem
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Sun, 12 Mar 2006 18:27:49 +0000 (UTC)
Fernando Barsoba said:
<snip>
TempEvent = malloc(1 * sizeof *TempEvent);
Allocations can fail. You don't test this, so...
double i=0;
srand(1);
for( i = 0; i < 20000; i++ ) {
TempEvent->codeEvent = i + 1;
....this statement invokes undefined behaviour if the memory allocation did
indeed fail. Now, if I recall correctly, you're experiencing problems with
your code only when you are dealing with lots of data - and it's precisely
at such a point that you are more likely to suffer from memory shortage. So
it's well worth paying attention to such details.
TempEvent->TimeExpire = rand()/1000000;
DLAddAfter(&List, 0, TempEvent, sizeof *TempEvent);
DLAddAfter() requires memory allocation and, again, this can fail. If so, it
will return DL_NO_MEM, which is defined in "dllist.h" as 1. Since you don't
check for this return value, you don't know whether the allocation
succeeded.
<snip>
// Generate intial event for 'Timer'
DLLIST * SetTimerT1(DLLIST *List, EVENT *TempEvent) {
TempEvent->codeEvent = 4;
strcpy(TempEvent->descEvent, "T1");
TempEvent->TimeInit = MC + Timer;
TempEvent->TimeExpire = 0;
DLAddAfter(&List, 0, TempEvent, sizeof *TempEvent);
Same applies here.
<snip>
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.
- References:
- Memory leak problem
- From: Fernando Barsoba
- Memory leak problem
- Prev by Date: Re: Multiple Assignment Evaluation Debate
- Next by Date: Re: give me some tips
- Previous by thread: Memory leak problem
- Next by thread: Re: Memory leak problem
- Index(es):
Relevant Pages
|