Description
From @hammadtq on April 9, 2015 10:31
SD.remove
works fine in the start but would stop functioning on itself, if I give a new file name, it will work perfectly fine again while leaving the old file as it is, the last file I encountered problem was of the 4086 bytes. The issue is very random, such as it will perfectly fine for a few days and then will give problem with one of the files.
I couldn't reproduce the condition under which it would start doing this, however, I tried to debug and trace the issue, here is what I found in SDVolume.cpp
in the method of
uint8_t SdVolume::freeChain(uint32_t cluster)
do {
uint32_t next;
if (!fatGet(cluster, &next)) return false;
// free cluster
if (!fatPut(cluster, 0)) return false;
cluster = next;
} while (!isEOC(cluster));
I noticed that next has a defined value when the first cluster is called such as maybe: 3911516415
However, when it gets through fatGet and fatPut, if we print the next value just before cluster=next, it appears to be 0, making the while to break and false to return on next loop iteration.
What I did to resolve is simple, I just saved the value of next in a new variable 'newnext' before going to fatGet and then assigned that 'newnext' to cluster at the end, this resolved the issue at once and file was deleted.
do {
uint32_t next;
uint32_t newnext = next;
if (!fatGet(cluster, &next)) return false;
// free cluster
if (!fatPut(cluster, 0)) return false;
cluster = newnext;
} while (!isEOC(cluster));
I hope this will help in finding and fixing the issue correctly.
Copied from original issue: arduino/Arduino#2946