0

I'm doing a program who get a line from the SD and shows it, this function is called getData()

FULL CODE:

#include <SD.h>
#include <MemoryFree.h>

File myFile;
int stringIndex = 0;
int cursorPosition = 0;
char inputString [25];
char inputChar;
boolean endOfLine;
char fullPath[11];

void setup(){

  Serial.begin(9600);

  Serial.print("Initializing SD card.");
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}

void loop(){

  Serial.println(getDataSD());

  Serial.print("freeMemory()=");
  Serial.println(freeMemory());
}

String getDataSD(){

  memset(inputString, 0, sizeof(inputString));

  getFileNameSD();

  myFile = SD.open(fullPath);

  if(myFile){
    myFile.seek(cursorPosition);

    endOfLine = false;    

    while(myFile.available() && endOfLine == false){
      inputChar = myFile.read();

      if(inputChar == '*'){
        while(inputChar != '\n'){
          inputChar = myFile.read();
        }
      }else{
        cursorPosition = myFile.position()-1;

        if(inputChar == 10 || inputChar == ' ') inputChar = myFile.read();

        while(inputChar != '\n'){
          if(stringIndex < 25){          
            inputString[stringIndex] = inputChar;
            stringIndex++;
            inputChar = myFile.read();
          }
        }
        inputString[stringIndex-1] = '\0';        
        stringIndex    = 0;
        endOfLine      = true;        
      }      
    }
    myFile.close();    
  }
  return inputString;
}

void getFileNameSD (){

  char folder[] = "/ACC";

  checkFolderSD(folder);  

  myFile = SD.open(folder);
  myFile = myFile.openNextFile();

  if(!myFile) myFile.rewindDirectory();

  if(myFile){

    strcpy(fullPath,folder);
    strcat(fullPath,"/");
    strcat(fullPath,myFile.name());

    myFile.close();
  }
}

I don't have problem with this function(getDataSD). To get the name of the file, I've created another function who looks for a file in a specific folder, this function is called getFileNameSD()

I'm having problem with this last function, it's not releasing or overwriting some data, so every time it's called, it is stored new data in memory

I've included the memory Free library to see where is the problem. When I call the getDataSD and write the file manually I dont have problem.

myFile = SD.open("/ACC/11001");

Serial monitor

freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/
freeMemory()=6592
/-27/-26/-56/

the memory always is the same.

but not when I call the getFileNameSD()

myFile = SD.open(getFileNameSD());

Serial monitor

freeMemory()=6571
/-27/-26/-56/
freeMemory()=6540
/-27/-26/-56/
freeMemory()=6509
/-27/-26/-56/
freeMemory()=6478
/-27/-26/-56/
freeMemory()=6447
/-27/-26/-56/

I know I'm doing something wrong with getFileName() but I don't know what is, can anybody help/explain me?

2
  • 1
    Where did you declare and define fullPath?
    – Ariser
    Nov 20, 2014 at 16:40
  • sorry about that, was typo, I corrected the first post
    – GEPD
    Nov 20, 2014 at 19:03

2 Answers 2

1

Ok,

I don't know, why your memory is leaking, but anyway, your program won't work safely. You defined an array (fullPath) inside the local scope of your function. You return it, which means, you return a pointer to this array. But at the same time, this array gets invalid, because you leave the local scope of your function.

Better define that string outside the function and give it as a parameter to the function. Then you aren't in danger of accessing an invalid pointer. Furthermore your memory leak won't appear.

1
  • thanks for the advice Arister, I've edited the code to fix that problem, unfortunately it didn't solved my problem.
    – GEPD
    Nov 20, 2014 at 23:47
1

Well, finally someone found where was the problem. In the getFileName() function, I open two instances of the file but I close just one.

here is the answer:

#include <SD.h>
#include <MemoryFree.h>

File myFile;
int stringIndex = 0;
int cursorPosition = 0;
char inputString [25];
char inputChar;
boolean endOfLine;
char fullPath[11];

void setup(){

  Serial.begin(9600);

  Serial.print("Initializing SD card.");
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    return;
  }
  Serial.println("initialization done.");

}

void loop(){

  Serial.println(getDataSD());

  Serial.print("freeMemory()=");
  Serial.println(freeMemory());
}

void getDataSD(){

  memset(inputString, 0, sizeof(inputString));

  getFileNameSD();

  myFile = SD.open(fullPath);

  if(myFile){
    myFile.seek(cursorPosition);

    endOfLine = false;    

    while(myFile.available() && endOfLine == false){
      inputChar = myFile.read();

      if(inputChar == '*'){
        while(inputChar != '\n'){
          inputChar = myFile.read();
        }
      }else{
        cursorPosition = myFile.position()-1;

        if(inputChar == 10 || inputChar == ' ') inputChar = myFile.read();

        while(inputChar != '\n'){
          if(stringIndex < 25){          
            inputString[stringIndex] = inputChar;
            stringIndex++;
            inputChar = myFile.read();
          }
        }
        inputString[stringIndex-1] = '\0';        
        stringIndex    = 0;
        endOfLine      = true;        
      }      
    }
    myFile.close();    
  }
}

void getFileNameSD (){

  char folder[] = "/ACC";
  File myDir = SD.open(folder);

  memset(fullPath, 0, sizeof(fullPath));

  if (myDir){
    myFile = myDir.openNextFile();

    if(myFile){
      strcpy(fullPath,folder);
      strcat(fullPath,"/");
      strcat(fullPath,myFile.name());
      myFile.close();
      myDir.close();
    }else{
      // Handle file not found
      myDir.close();
      fullPath[0] = '0';
    }
  }else{
    // Handle directory not opened
    fullPath[0] = '0';
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.