Go Down

Topic: I need help with memory leak (reading from SD-card) [solved!] (Read 2075 times) previous topic - next topic


Sep 24, 2012, 09:50 pm Last Edit: Sep 25, 2012, 09:09 am by Zkronk Reason: 1
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: [Select]
// 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/
// 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;
     inData[index] = '\0';
// When all characters are read, close the file    

// If the internet connection to the server is succeeded, it will upload the char-array "inData"
   if (client.connect(server, 80)) {
   SD.remove(filePath); // Then delete the buffer-file from the SD-card  



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!  XD

I didn't close the File-class "fileName" which was eating up the Megas' SRAM...
Code: [Select]

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131