Thanks, Fernando.  The actual code in my real application does a lot more 
stuff.  This is just an example code to illustrate the leak that I don't 
expect to occur.

valgrind does not report any leak.

Khoa.



From:
Fernando Campos <[email protected]>
To:
Khoa To <[email protected]>
Cc:
[email protected]
Date:
06/21/2010 10:41 AM
Subject:
Re: Mem leak with fopen/fseek/fclose ?
Sent by:
[email protected]



Hi there!!

I would try to run your program with valgrind, a memory checker tool 
really useful. I'll probably need to recompile with -g flag.

Have you checked that MAX_INT (limits.h) is bigger than 300000??? I guess 
it is, cause other way you would never get the "Success" message.

Let me make some comments on your code:


If I comment out the 2 fseek statements, then I don't see any memory
increase.

The file size is about 700KB.

I was wondering if someone can tell me if this is a known issue or if I'm

doing something wrong.

Thanks,
Khoa.

======Source file, compiled with gcc=========

#include <stdio.h>
#include <errno.h>

int main()
{
  FILE * ptr = NULL;
  int count = 0;

  ptr = fopen("myBinaryFile","r");
 if(ptr=NULL){
perror("fopen");
return ERROR;
}

  while (ptr != NULL &&amp; count++ < 300000)
  {
       usleep(1000);

       if (fseek(ptr, 0, SEEK_END))
               
{
 
 //printf("Error 1: %d\n", errno);
perror("fseek");
break;
}
       if (fseek(ptr, 0, SEEK_SET))
               
{
//printf("Error 2: %d\n", errno);
perror("fseek");
break;
}
/*Since you are using the errno.h library, you should use the perror 
function, since it's the function provided by errno.h and uses the 
standard error output, which is unbuffered, unlike printf, which uses 
stdout so the messages you print could be buffered (but you should see any 
error messages since you are ending your error messages by "/n" and 
anyway, any buffered message should be printed before the "Success").*/

       /*if (fclose(ptr))
               break;*/
 if(ptr){ //Check ptr is not NULL
if(fclose(ptr)==EOF){
perror("fclose");
return ERROR;
}
ptr=NULL;
}

       usleep(10000);

       ptr = fopen("myBinaryFile","r");
if(ptr==NULL){
perror("fopen");
return ERROR;
} 
  }

   //fclose(ptr);
if(ptr){ //Check ptr is not NULL
if(fclose(ptr)==EOF){
perror("fclose");
return ERROR;
}
ptr=NULL;
}

   if (count >= 300000)
      print("Success\n");

   return 0;
}


BTW, why are you opening your file with the "r" mode, which set the stream 
position at the beginning of the file, then move the stream position to 
the end of the file, and then move it again to the beginning? Why do you 
close and open the file in each iteration of the while loop?? How can you 
see your program is using 6MB after execution?? If your program prints 
"Success" and executes "return 0;", the OS should release all the process 
resources.


I think your best option is to use valgrind, but I don't understand the 
sense of your code...

Good luck.

Fernando.

-- 
---------------------------------------------------------------------------------------------------------
Fernando Campos Del Pozo
Becario Super-computación
Departamento de Física Teórica
Universidad Autónoma de Madrid
Tlf.: +34-914974893
e-mail: [email protected]
http://rincon.uam.es/dir?cw=999890136718750


Reply via email to