PowerBroker2 / SafeString

This SafeString library is designed for beginners to be a safe, robust and debuggable replacement for string processing in Arduino. Note, this is NOT my work, I am simply hosting it for easy access. The original code belongs to Forward Computing and Control Pty. Ltd.

Home Page:https://www.forward.com.au/pfod/ArduinoProgramming/SafeString/index.html

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SafeString 4.1.5 causing SSD1306 OLED failure to allocate

tradJack opened this issue · comments

I have been using safestring successfully for over a year.. however lately, I have gotten bogged down with strange interactions when using SafeString AND Adafruit SSD1306 OLED 128x64 display..

attached is the demo file from the adafruit library with simply safestring included and if u uncomment the several createSafeStrings, it causes the display to not allocate..

??????

regards-- jack
ssd1306_128x64_i2c_SafeString.zip

commented

Could not reproduce issue
Installed Adafruit GFX Library version=1.10.9
Adafruit BusIO version=1.7.3
Adafruit SSD1306 version=2.4.5
SafeString version=4.1.5

Modified ssd1306_128x64_i2c_SafeString.ino setup to

void setup() {
  Serial.begin(9600);
  for (int i = 10; i > 0; i--) {
    Serial.print(i); Serial.print(' '); delay(500);
  }
  Serial.println();  
  SafeString::setOutput(Serial);
  if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
  Serial.println(F("Passed allocation"));

Output on Mega2560 was

10 9 8 7 6 5 4 3 2 1 
Passed allocation
commented

Under investigation
Can reproduce on Uno when modified supplied program to un-comment

createSafeString(tmpStr,50);
createSafeString(captureData,10);
createSafeString(camStr,4);
createSafeString(vgStr,4);
createSafeString(volStr,4);
createSafeString(field,10);
commented

Immediate work around is to replace the global SafeStrings with char[ ]

char tmpStr_c[50 + 1];
char captureData_c[10 + 1];
char camStr_c[4 + 1];
char vgStr_c[4 + 1];
char volStr_c[4 + 1];
char field_c[10 + 1];

Then at the top off the loop() add

  cSFA(tmpStr, tmpStr_c);
  cSFA(captureData, captureData_c);
  cSFA(camStr, camStr_c);
  cSFA(vgStr, vgStr_c);
  cSFA(volStr, volStr_c);
  cSFA(field, field_c);

to work with them as SafeStrings

commented

Close Issue as no error.

Required memory exceeds the available RAM.
The display requires 1024 bytes
With the global SafeStrings defined and with the local variables in setup() the remaining memory is ~1081 on UNO.
malloc on Arduino AVR needs free space of 128 to succeed.
1024 + 128 > 1081 so the allocation fails.
commenting out the
createSafeString(tmpStr,50);
allows the allocation to succeed.
With SafeString #define SSTRING_DEBUG (the default)
One SafeString takes about 53bytes + length of the SafeString variable name + length
Subsequent SafeStrings take about 30bytes + length of the SafeString variable name + length
so removing the createSafeString(tmpStr,50); statement
frees up about 80 bytes (since there are still other SafeStrings defined) which is enough to allow the display allocation to succeed.