Hello all

 

I am facing a strange problem.... Memory leak during file write to a USB
memory stick.

 

We are using uClinux2.6.17 on coldfire 5372. The memory card is mounted
on /hom/usb directory. I am trying to run the following simple code
which involves copying a file to different files on the USB storage
device.

 

Code : 

 

#define BUFSZ 300000

int FileCopy(const char *src, const char *dst ) 

{

 

      char            *buf2;

      FILE            *fi;

      FILE            *fo;

      

      unsigned    int    amount;

      unsigned    int    written;

      int             result;

 

      buf2 = (char *)malloc(BUFSZ);////new char[BUFSZ]; #if 1

      fi = open( src, O_RDONLY );   

      fo = open( dst, O_CREAT | O_WRONLY, S_IWRITE );

 

      result = 1;

      

      if((fi == -1) || (fo == -1) )

      {

              result = -1;

              if (fi != -1) close(fi);

              if (fo != -1) close(fo);

      }

      

      amount = read( fi, buf2, BUFSZ);

      printf("amount:%d\n", amount);      

      

      written = write(fo, buf2, amount);

      printf("amount:%d\n", written);

      

      close(fi);

      close(fo);  

      

      

      system("free");

      

      return(result);

 

}

 

 

int main()

{           

      char src[255] = "file2.m4v";

      char dst[255];

      int i = 0;  

      long delay; 

      

      while(1)

      {

          sprintf(dst, "/home/usb/Flv%d", i++);       

          if (FileCopy( src, dst ) != 1)

          {

                  printf("Failed to copy file\n");

          }

          else

          { 

                  printf("Copied file%d\n", i);

          }       

          delay = 0x100000;

          while(delay--);

      }

      

  return 0;

}

 

 

The problem is whenever I run this code I observe following output:

 

 

              total         used         free       shared      buffers

  Mem:        62276         9912        52364            0         1536

Copied file9

amount:271005

amount:271005

 

              total         used         free       shared      buffers

  Mem:        62276        10212        52064            0         1536

Copied file10

amount:271005

amount:271005

 

              total         used         free       shared      buffers

  Mem:        62276        10500        51776            0         1536

Copied file11

amount:271005

amount:271005

 

 

cat /proc/meminfo

MemTotal:        62276 kB

MemFree:         51800 kB

Buffers:          1536 kB

Cached:           3764 kB

SwapCached:          0 kB

Active:           1680 kB

Inactive:         3592 kB

HighTotal:           0 kB

HighFree:            0 kB

LowTotal:        62276 kB

LowFree:         51800 kB

SwapTotal:           0 kB

SwapFree:            0 kB

Dirty:            2948 kB

Writeback:           0 kB

Mapped:              0 kB

Slab:                0 kB

CommitLimit:     31136 kB

Committed_AS:        0 kB

PageTables:          0 kB

VmallocTotal:        0 kB

VmallocUsed:         0 kB

VmallocChunk:        0 kB

 

 

              total         used         free       shared      buffers

  Mem:        62276        10800        51476            0         1536

Copied file12

amount:271005

amount:271005

 

              total         used         free       shared      buffers

  Mem:        62276        11088        51188            0         1536

Copied file13

amount:271005

amount:271005

 

It can be observed that Used memory increases by about 300KB each time I
do a malloc and fwrite and finally the kernel crashes when the used
memory reaches around 30MB. I am not sure about what is the problem. I
googled around it but I couldn't find much. Could anyone help me in this
regard?   

_______________________________________________
uClinux-dev mailing list
[email protected]
http://mailman.uclinux.org/mailman/listinfo/uclinux-dev
This message was resent by [email protected]
To unsubscribe see:
http://mailman.uclinux.org/mailman/options/uclinux-dev

Reply via email to