Why does getting list of files from SD card FAIL if not printing to Serial? (ESP8266) I've got this piece of code for reading a list of files from root directory on SD card, which I run on ESP8266 chip (maybe it makes a difference):

  File dir = SD.open("/", O_RDONLY);

  int n = 0;
  while(true)
  {
     File entry =  dir.openNextFile();
     if (!entry) break;
     if (!entry.isDirectory())
     {
       Serial.println(entry.name()); 
       Serial.flush();
       n++;
     }
     entry.close();
  }
  dir.close();
  Serial.println(n);


The code works fine, but can someone please tell me why, when I comment out printing file names to the Serial in this loop, the program crashes? How is this possible? Are the files opened too rapidly or what? And how can I get the list, let's say into a vector&lt;String&gt; without printing it to the Serial?

When I comment out the printing the chip crashes with this in output:

ctx: cont 
sp: 3ffeb540 end: 3ffeb950 offset: 01b0

&gt;&gt;&gt;stack&gt;&gt;&gt;
3ffeb6f0:  000000ff 00007c52 0000012c 3ffea30c  
3ffeb700:  000000c0 3ffea4dc 00000000 40203a70  
3ffeb710:  00000040 000000ff 3ffea318 402042f7  
(...)
3ffeb940:  00000000 00000000 3ffea930 40100378  
&lt;&lt;&lt;stack&lt;&lt;&lt;
ň"_Çc_ÇĎ
Try to use "delay(0);" in the loop (or yield() maybe).