Using the new Arduino IDE for ESP8266 and found bugs, report them here

Moderator: igrr

User avatar
By teddyz
#85501 Hi

I need a temporary character array in the function below. I saw that there is a new way to do it and I decided to try. When I run this code with arduino_new, my sensor is not behaving good. If I instead run old new-command it works. I just want to know if I use the command correctly or if there is something that is wrong with arduino_new.

arduino_new is documented here https://arduino-esp8266.readthedocs.io/en/2.6.3/reference.html#c

/Dick

Code: Select allvoid secureRepublishPUSHOVER()
{
  char *fname;
  static int8_t res = 99;
  for ( uint8_t i = 0; i < 3; i++ ) {
    //ESP.wdtFeed();
    fname = savedSecureFileName("PO");
    if ( strlen(fname) ) {
      File f = SPIFFS.open( fname, "r" );
      size_t fsize = f.size();
      fsize = min( fsize, (size_t)1024);
      if ( fsize ) {
        //char *fcontent = new char[fsize + 1]; // works
        char *fcontent = arduino_newarray( char, fsize + 1); //makes the program behave strange
        if ( fcontent == NULL ) {
          log( 3, F("Gick inte att allokera minne i secureRepublishPUSHOVER(). Tog bort filen") );
          SPIFFS.remove(fname);
          return;
        }
        f.readBytes( fcontent, fsize );
        f.close();
        fcontent[fsize] = 0;  //terminate text
        res = publishPUSHOVER ( MQTT_CLIENTID " - RESEND", fcontent, -1 );
        delete [] fcontent;
        if (res == 1 ) {
          SPIFFS.remove(fname);
        }
        else
        log ( 2, p("secureRepublishPUSHOVER() res=%d", res) );
          return;
      }
      else
        f.close();
    }
  }
}

char *savedSecureFileName (const char *directory) {
  uint32_t startTime = millis();
  static char fname[32];
  Dir dir = SPIFFS.openDir(directory);
  if (dir.next()) {
    if ( dir.fileSize() ) {
      dir.fileName().toCharArray(fname, sizeof(fname) - 1);
      if ( strstr(fname, ".secure" ) )  {
        return fname;
      }
    }
  }
  fname[0] = 0;
  return fname;
}