Solved Possible Bug in SD Library use

testErrorCase.ino (19.4 KB)

The code crashes early in the setup() processing. If I were to guess I would say somethings up with either the compiler or cleanup or running out of stack.

I'm learning how to use the SD library. To that end I wrote a test case for much of the library functions. When getting to near the end of learning the code broke. It worked up to adding the last file.write() testing.

If one comments out line 414 "binaryLongWrite();" all the code works, see above code. After trying all sorts of changes and assuming it was my code I tested line 414 function call it a different environment, see code below. This works as expected.

testSDWriteBinary.ino (2.5 KB)

/************** display monitor results where it breaks *****************/

SD.begin()... Initializing SD card...initialization done.

Check if MYDIR Exists...MYDIR Doesn't Exists, creating... SUCCESS

Print File Name BEFORE CREATED:

Checking if Files Exists...
Does Not Exists, create it
Failed to CREATE

Use of Strings can cause AVR based Arduinos to crash or malfunction, and they are never needed. First thing to do would be to get rid of them.

By removing a section of code in the setup() it works. The code remove was the first binary write and read long word from a file.

Seems to support some kind of compiler or stack overflow.

const String MY_SD_CARD_DIRECTORY = "MYDIR";
const String BINARY_FILENAME = "binary.bin";
const String TEXT_FILENAME = "text.txt";
const String BINARY_PATH_FILENAME = MY_SD_CARD_DIRECTORY+"/"+BINARY_FILENAME;
const String TEXT_PATH_FILENAME = MY_SD_CARD_DIRECTORY+"/"+TEXT_FILENAME;

so replace these with what ?

Ordinary, reliable C-strings, character arrays, e.g.

char binary_filename[] = "file.dat";

Replace lines like this:

Serial.println("\nREMOVE "+BINARY_FILENAME+" SUCCESS");

with

Serial.print(F("\nREMOVE "));  //USE THE F MACRO to save ram
Serial.print(binary_filename);
Serial.println(F(" SUCCESS"));

The problem you are having is almost certainly NOT with the SD library.

Do you see warnings when you compile? (Activate the option for all warnings in the preferences)

C:\Users\dana_\Desktop\Arduino MySketch Library\SDCardIntroduction\testErrorCase\testErrorCase.ino: In function 'void setup()':
C:\Users\dana_\Desktop\Arduino MySketch Library\SDCardIntroduction\testErrorCase\testErrorCase.ino:299:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i<fileRdTxt.size(); i++) {
~^~~~~~~~~~~~~~~~~
C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp: In function 'open.constprop':
C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp:462:14: warning: 'pathidx' may be used uninitialized in this function [-Wmaybe-uninitialized]
filepath += pathidx;
^
C:\Program Files (x86)\Arduino\libraries\SD\src\SD.cpp:456:9: note: 'pathidx' was declared here
int pathidx;
^

I'll give this a try and see what happens.

It is always best to create the minimal test program that recreates the issue. With the SD libraries, a big chunk of RAM memory is already allocated to the SD card buffer, so memory issues become more likely.

Rather than add to a long, existing test program, write code to test just the features that you will be using. Or, eliminate the existing tests that are irrelevant.

Okay then... I replaced all of the strings as suggested and it now works.

I tried running a couple of times by replace one String. Still failed. Did the next still failed. Replaced them all now it works.

What I find odd, the const Strings, even though unused, sucked up 58 bytes of memory. I would have thought the compiler would just discard.

@jremington Thanks for your help!

It now works as expected..

testErrorCase.ino (16.4 KB)

This file has many examples SD library functional use. A bit long but perhaps someone will find it useful.

Yaay! Glad to hear it.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.