anilgkts / arduino

Automatically exported from code.google.com/p/arduino
Other
0 stars 0 forks source link

SD examples don't close file handles #832

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I've only checked the list example, but if there are more than 26 files on a 
card, it fails. (only lists the first 26 files on the card, including 
directories). I recreated it by:
touch 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 etc... 36
Here's the output:
Initializing SD card...initialization done.
1       0
2       0
3       0
4       0
5       0
6       0
7       0
8       0
9       0
10      0
11      0
12      0
13      0
14      0
15      0
16      0
17      0
18      0
19      0
20      0
21      0
22      0
23      0
24      0
25      0
done!
after applying the fix (attached)
Initializing SD card...initialization done.
1       0
2       0
3       0
4       0
5       0
6       0
7       0
8       0
9       0
10      0
11      0
12      0
13      0
14      0
15      0
16      0
17      0
18      0
19      0
20      0
21      0
22      0
23      0
24      0
25      0
26      0
27      0
28      0
29      0
30      0
31      0
32      0
33      0
34      0
35      0
36      0
done!

The reason is that the program runs out of file handles. It keeps 
openNextFile()-ing, but never closes them.
attached is a fixed example.

What version of the Arduino software are you using?
Arduino1.0
On what operating
system?
Windows 7
Which Arduino board are you using?
Uno R2

Please provide any additional information below.

Original issue reported on code.google.com by kevin.os...@gmail.com on 27 Feb 2012 at 2:19

Attachments:

GoogleCodeExporter commented 9 years ago
Just noticed this with 1.0.1. It's a simple fix, and only seems to be needed in 
the lisfiles example.

Probably a result of pre 1.0 not supporting multiple open files.

Original comment by j...@blackcatmicro.com on 30 May 2012 at 6:57

GoogleCodeExporter commented 9 years ago
see also issue 904
http://code.google.com/p/arduino/issues/detail?id=904

Original comment by j...@blackcatmicro.com on 30 May 2012 at 6:59

GoogleCodeExporter commented 9 years ago
The resolution is simple and does not require any modifications to SD.cpp.
Simply add entry.close() to the example code as shown below.

Gary

void printDirectory(File dir, int numTabs) {
    while(true) {

     File entry =  dir.openNextFile();
      if (! entry) {
        // no more files
        // return to the first file in the directory
        dir.rewindDirectory();
        break;
      }
      for (uint8_t i=0; i<numTabs; i++) {
        Serial.print('\t');
      }
      Serial.print(entry.name());
      if (entry.isDirectory()) {
        Serial.println("/");
        printDirectory(entry, numTabs+1);
      } else {
        // files have sizes, directories do not
        Serial.print("\t\t");
        Serial.println(entry.size(), DEC);
      }
      entry.close();
    }
 }

Original comment by garygfle...@hotmail.com on 30 Oct 2012 at 8:48

GoogleCodeExporter commented 9 years ago
fixed on master. thank you!

Original comment by federico...@gmail.com on 20 Dec 2012 at 3:30

GoogleCodeExporter commented 9 years ago

Original comment by federico...@gmail.com on 20 Dec 2012 at 3:55