Pages: [1]   Go Down
Author Topic: I need help with memory leak (reading from SD-card) [solved!]  (Read 1540 times)
0 Members and 1 Guest are viewing this topic.
Sweden
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello!
I have come to a end that I realise that I need help from someone else to look at my code to see with new eyes what could be wrong.
The function of it is to read buffered text-files from the SD-card and then try to upload it to my webserver, and if it succeeds it deletes that file and takes on another one if there still are files left to upload.

It works exactly as it is designed to do, except that it has some kind of memory leak.
From start I have 4232 bytes of free SRAM on my Mega 2560, and when this upload-function has done it's work it's down to only 220 bytes free SRAM.
I suspect that it has something to do with it not properly closing the files that have been opened or so. But I have been scratching my head for some time now without getting any results.

The size of the buffer-files are less than 100 characters, and I've been running tests with about 40-60 files at most.
So the big question is how do I get the SRAM used by it released again?

I use the SD.h library included in the Arduino 1.0.1 IDE.

Code:
// Runs the following codeblock every night 00:02, for one minute.
// If there are any upload-data buffered on the SD-card it will upload it and then delete it from the card.
if (hour == 00 && minute == 02) {
  
// Opens folder /upload/ on the SD-card and opens the first file in that folder  
 File uploadFolder = SD.open("/upload/");  
 File fileName = uploadFolder.openNextFile();
 
// When there still are files left in the folder, set the char-array "filePath" to the file to upload
 if (fileName) sprintf(filePath, "/upload/%s", fileName.name());

// Close the folder /upload/
 uploadFolder.close();
    
// If the file with path "filePath" exists on the SD-card, open it
if (SD.exists(filePath)) {    
    File uploadData = SD.open(filePath);

// Resets the index variable to 0, so the next codeblock will start filling the char-array "inData" from the first position
index = 0;

 if (uploadData) {
// When there still are characters to read, put them in the char-array "inData" one by one (and not more than 99 of them)
    while (uploadData.available() && index < 100) {
      inChar = uploadData.read();
      inData[index] = inChar;
      index++;
      inData[index] = '\0';
    }
// When all characters are read, close the file    
    uploadData.close();
  }

// If the internet connection to the server is succeeded, it will upload the char-array "inData"
    if (client.connect(server, 80)) {
    client.println(inData);
    client.println();
    client.stop();
    SD.remove(filePath); // Then delete the buffer-file from the SD-card  
  }    
 }  
 
}
« Last Edit: September 25, 2012, 02:09:09 am by Zkronk » Logged

Sweden
Offline Offline
Newbie
*
Karma: 1
Posts: 4
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I found the solution, I've been reading my post here at the forums a couple of times and it was easier to read when it was separated from the rest of the code!  smiley-lol

I didn't close the File-class "fileName" which was eating up the Megas' SRAM...
Code:
fileName.close();
Logged

Pages: [1]   Go Up
Jump to: