Serious bug affects SD.h and String

There is a serious bug in 1.01 and some earlier Arduino versions that may cause crashes when you use SD.h and/or String.

The bug is in the function free() which deallocates memory.

SD.h calls free() when you close a file.

String may call free() when the size of a string changes or in destructors.

Frequent opening and closing of files, especially multiple file, may cause a crash in SD.h.

See this for an example of the String problem,115552.0.html.

The above topic also describes two fixes for the free() bug.

The SdFat library does not call free(). The bug is in the Arduino SD.h wrapper for SdFat.

Yes, String does not do a good job of recovering memory. If in loop(0, the free memory reduces each cycle until it crashes. However, the File library, included in SD, is OK to create an instance for each write, so that each record can open and close. My problem is with SD's SRAM usage, needing 400 bytes free before making a record. In my latest project (see My first Ardurino project) I had to use PROGMEM to cut my SRAM footprint. Since I had to write a custom LCD class anyway,(because I used a shift register) I put the excess code to display a string from the program memory in a method.

It's not a matter of doing a good job.

The core Arduino software has a serious bug in dynamic memory allocation so any library that uses dynamic memory may crash even if very little memory is used.

The bug involves how the free list is maintained. So SD or String can suddenly crash with very little memory in use.

So SD or String can't expectantly crash with very little memory in use.

They can't? I thought that was what the bug caused to happen.

Sorry PaulS major proof reading error.

free() can really jumble the free list and then sometime later this mess leads to a crash.