Memory leak in openNextFile()

Looking for a solution to the memory leak in openNextFile()...

When at the end of a directory, the function returns null, but 32 bytes fall into the trans-dimensional vortex, never to be seen again (until reset).

From what I understand, the memory is malloc'd when a file is opened, but if it is destroyed before closing, the allocation is lost. In this case, the file is never really opened, so of course it can't be closed.

To make sure I wasn't just being a knucklehead, I checked by adding a FreeRam() call before and after the printDirectory() call in the "listfiles" example of the SD library. The issue is present, repeatable and deterministic in the example program as well. My SD card has 3 directories. The free RAM before the printDirectoy() call was 1001B and it was 905B after. Add a directory, and 32B additional memory is gone.

I'll try to track this down in the library, but I'm a noob so I'm not especially hopeful... :stuck_out_tongue:

This is a well known problem with the listFiles example and has been around for more than a year.

In general memory is lost unless a File object is closed before doing another open.

This is due mostly to poor design of the SD.h wrapper for SdFat.

Maybe at some point the Arduino company will fix it.

You can find many posts about this problem in the forum.