Re: Memory leak problem



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)
.



Relevant Pages

  • Re: Surrogate Factoring Solution
    ... > find myself holding on to it, versus just junking ideas that fail. ... > time to do the chunking. ... No one is trying to control your behavior. ...
    (sci.math)
  • Re: Surrogate Factoring Solution
    ... > find myself holding on to it, versus just junking ideas that fail. ... > time to do the chunking. ... No one is trying to control your behavior. ...
    (sci.crypt)
  • Re: I need help please!
    ... acknowledge it COULD fail, possibly, maybe, and possibly maybe ... death at the keyboard with your cheek hitting CTRL and your nose ... So people who point out your mistakes are trolls. ...
    (comp.lang.c)
  • Re: Printing files every step
    ... snip ... ... your code is 100% correct from the coding and logic points of view. ... Does "the user has to close files properly" imply that if I fail to ... Note that the standard says that exit() closes all open streams (and ...
    (comp.lang.c)
  • Re: newbies need help interpreting Diehard output
    ... Cipher design is much harder than designing a pseudorandom number ... generator that will pass the Diehard tests. ... test seems to fail while others appear to pass with flying colors. ...
    (sci.crypt)