ESP32 - Stack smashing protect failure!

Hello friends.

I am coding on a part where i can log data on my SD card. I am using an Nextion display. The following code shows a button where when i push it, it starts to log my data on my SD card, or stops it. Relative simple

Everything works so far, but sometime i get the coredump with the warning Stack smashing protect failure!

void datalogOnOffPopCallback(void *ptr)
{
  uint32_t t_datalogState;
  datalogOnOff.getValueDS("Datenlogging",&t_datalogState);
  uint8_t cardType = SD.cardType();

  if (t_datalogState==1)
  {
    if (cardType == CARD_NONE || !rtc.begin())
    {
      Serial.println("SD is not insertedlllllllllzzzzzzzzzzzzzzz");
      sonde_pH._datalogState = 0;
      //Serial.println("A");
      EEPROM.writeInt(PHVALUEADDR + 9*(sizeof(float)), sonde_pH._datalogState);
      // Serial.println("B");
      EEPROM.commit();
        //Serial.println("C");
      delay(15);
      // Serial.println("D");
      datalogOnOff.setValueDS("Datenlogging",sonde_pH._datalogState);
        //Serial.println("E");
      delay(15);
      SD.begin(5);
      return;
        //Serial.println("F");
      //ESP.restart();
    }
  }

  String fileName = String();
  if (t_datalogState==1)
  {
  sonde_pH._filenumber++;
  }
  fileName = "/file_";
  fileName += sonde_pH._filenumber;
  fileName += ".txt";
  Serial.print("mmmmm ");
  Serial.println(sonde_pH._filenumber);
  char charFileName[fileName.length() + 1];
  sonde_pH._charFileName[fileName.length() + 1];
  fileName.toCharArray(charFileName, sizeof(charFileName));
  fileName.toCharArray(sonde_pH._charFileName, sizeof(sonde_pH._charFileName));

  if (t_datalogState==1)
  {
    Serial2.print("vis Main_front.log,1");
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);

    Serial2.print("vis Main_front.logname,1");
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);

    writeFile(SD, charFileName, "ID, Date, Time, Value_raw, Value_maped \r\n");
    EEPROM.writeInt(PHVALUEADDR + 11*(sizeof(float)), sonde_pH._filenumber);
    EEPROM.commit();
  }
  else
  {
    Serial2.print("vis Main_front.log,0");
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);

    Serial2.print("vis Main_front.logname,0");
    Serial2.write(0xFF);
    Serial2.write(0xFF);
    Serial2.write(0xFF);
  }

  if(t_datalogState!=sonde_pH._datalogState)
  {
    sonde_pH._datalogState=t_datalogState;
    EEPROM.writeInt(PHVALUEADDR + 9*(sizeof(float)), sonde_pH._datalogState);
    EEPROM.commit();
  }
  Serial.print("sonde_pH._datalogState: ");
  Serial.println(sonde_pH._datalogState);
  delay(100);
  if(sonde_pH._datalogState == 1)
  {
    //ESP.restart();
  }
}

This is the message i get:

Stack smashing protect failure!

abort() was called at PC 0x400db87c on core 1

Backtrace: 0x4008c6dc:0x3ffb1dc0 0x4008c90d:0x3ffb1de0 0x400db87c:0x3ffb1e00 0x400d37df:0x3ffb1e20 0x400d2bce:0x3ffb1e50 0x40116fa5:0x3ffb1ea0 0x400d40aa:0x3ffb1ec0 0x400d3bc2:0x3ffb1ee0 0x400d3097:0x3ffb1f10 0x400d89ed:0x3ffb1fb0 0x40088e19:0x3ffb1fd0

Rebooting...

Anyone an idea where my mistake is?

Thank you very much

What type is sonde_pH?

sonde_pH is just an object of my class to enter the subjects:

class Initialize
{
public:
  //Allgemein
  uint32_t _measrueIntvl;
  uint32_t _brightness;
  //Alarm
  uint32_t _alarmState;
  String _upperAlarm;               
  String _lowerAlarm;               
  //Datalog
  uint32_t _filenumber;
  uint32_t _readingID;
  uint32_t _datalogState;
  //Keylock
  uint32_t _keylockState;
  char _charFileName[50];
  //Messsignal
  float pHSignal;
  float pHSignal_smoothed;
  float pHValue;
  void GetMeasuredSignal(); 
  Initialize();
  ~Initialize();                                         
  void beginInitialize();
  void beginInitializeSendToNextion();                         
  void writeIntoNextion_MeasureRoutine();                        
  //writeIntoCalibration_CalibrationRoutine();                 
  //writeIntoCalibration_WatchdogRoutine()                        
  void logSDCard();

private:
};

thank you very much

Then what do you think this line does?

sonde_pH._charFileName[fileName.length() + 1];

Your problem may not be in the posted code, the posted code may simply be what provokes the problem. Please look at this.

LightuC:
Then what do you think this line does?

Nothing, the name of the file is hardly > 50 characters and the line is most likely optimized away.

thank you for your answer

sonde_pH._charFileName[fileName.length() + 1];

this line determines the needed memorysize of my char.

So i guess u mean the problem is that i am already defined the size of my char with
char _charFileName[50]; in my class and thats causes the problem.?!

So it would be better to define it like that in my class

class Initialize
{
public:
...
...
...
char _charFileName;
}

Am i on the right track?

Manuel_o:
Am i on the right track?

No, you can only use the array initializer where the array is declared. If you are sure that the length of the file name never exceeds 49 characters (plus null terminator) then the declaration of "char _charFileName[50]" is not the problem.

What I do not understand is why you need the file name as a char array twice?