Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By McChubby007
#80292 Hello everyone, I'm not used to using Spiffs so now I find myself using it and during some testing there is a memory leak in my code. Normally I would deal with this myself but I am unfamiliar with the Spiffs library and I am questioning my own code - is my code wrong, or is it the Spiffs library leaking? I don't know.

The issue is when I call my own function which opens a spiffs file, reads and prints its contents and then closes the file - sometimes memory is leaked and other times it is not.

Here is my code snippet, with notes annotated :

Code: Select all          void spiffsFileCat(const char *fileName) {

            File spiffsFile = SPIFFS.open(fileName, "r");

            size_t size = spiffsFile.size();
            //char rdBuf[size + 1];
            char *rdBufP = (char *)(malloc(size+1));

            spiffsFile.readBytes(rdBufP, size);   // possible leak here

            *(rdBufP+size) = 0;  // eol

            Serial.printf("'%s'\n", rdBufP);

            free(rdBufP);  // I have checked that free() releases the correct amount and it is ok.

            SpiffsFile.close();   // possible leak here

// NOTE : When this leaks, it does not leak the whole amount, some RAM
//        is freed after the spiffs.close() - 96 bytes freed, 320 lost

         }

void someFunc(void) {

   // 42688 free
   spiffsFileCat("/file1.txt");
   // 42688 = OK
   // Free RAM correct here

   spiffsFileCat("/file2.txt");
   // now 42368
   // (old 42688 - new 42368 = 320 bytes lost)

   spiffsFileCat("/file3.txt");
   // now 41816
   // (old 42368 - new 41816 = 368+184 = 552 nore bytes lost)

   // NOTE :
      // If I vary the above calls to the following (using same file name each
      // time) :
         // spiffsFileCat("/file1.txt");
         // spiffsFileCat("/file1.txt");
         // spiffsFileCat("/file1.txt");
   // Then 320 bytes is lost after 2nd call, but no more RAM is lost after 3rd call.
}


Do I need to delete the File object? I suspect not since no examples I've seen do that. It is odd that the first time through there is no leak, the 2nd call leaks and then the 3rd call may or may not be OK (see comments in code).

Any help or feedback is gratefully received.