Controller restart by accident

Hey guys,

I have a problem with (at least) one row in my code. My intention is to instantiate some objects for HTTP requests. I store their pointers in an array requestsBatch because I will call them from within an helper function. After storing them I’ll do some time checks and then send the request to the server. After a successful request the object will be deleted.

It seems the variable address is problematic but I can’t see why. I hope someone can brighten up the darkness in my brain… thank you!

const char* root_ca_sherz = "myCert";
    
    int currentRequestsBatchIndex=0;
    class HTTPREQUESTS {
      public:
        HTTPREQUESTS(String strAddress = "", String strParameters = "", bool bSendImmediately=false, const char* cert=root_ca_sherz) {
          address = strAddress;
          parameters = strParameters;
          certificate = cert;
          currentRequestsBatchIndex++;
    
    Serial.println("New object instantiated");
    Serial.println(address);
    Serial.println(parameters);
        }
    
        ~HTTPREQUESTS() {
          currentRequestsBatchIndex--;
        }
    
        bool sendRequest() {
          Serial.println("Called from within sendRequest()");
    
    
    
    Serial.println(address); // <<<<<< THIS ROW CAUSES THE REBOOT
    
    /*
          http.begin(address+"/"+parameters, certificate); //, root_ca_sherz
          int httpCode = http.GET();
    
          Serial.print("HTTP Code: ");
          Serial.println(httpCode);
          Serial.print("Payload: ");
          Serial.println(http.getString());
    
          if (httpCode > 0) { //Check for the returning code
            return true;
          }
    
          else {
             Serial.println("Error on HTTP request");
             return false;
          }
          http.end();
    
          delay(1000);
           
          return false;
    */
    return true;
        }
    
    
      private:
        const char* certificate="";
        String parameters;
        String device;
        String address;
        unsigned long timestamp=0;
        int sendAttempts=0;
        bool sendImmediately = false;
        unsigned long lastSendAttemp=0;
    };
    
      HTTPREQUESTS *requestsBatch[5];
      
    
    
    
    
    void setup() {
      // put your setup code here, to run once:
      Serial.begin(115200);
      delay(3000);
      Serial.println("Ready");
    
      String address = "https://myAddress.com";
      String parameters = "?secret=XXXXX&deviceName=deviceName&status=1&value=999&time=123456789&functionName=functionName";
    
    
      HTTPREQUESTS *req01 = new HTTPREQUESTS(address, parameters);
    
      // Store pointer to array to use it later
      requestsBatch[currentRequestsBatchIndex] = req01;
      Serial.println("Object stored in array");  
      Serial.print(F("Send request: "));
      if(requestsBatch[0]->sendRequest()) {
        //delete requestsBatch[0];
        Serial.println("requestsBatch[0] deleted");
      }

    }
    
    void loop() {
      // put your main code here, to run repeatedly:
    
    }

First of all, as far as I know, you have to define your function in the header, if you want to use optional parameters. Check this thread here https://forum.arduino.cc/index.php?topic=298351.0

This might be the problem actually, since your optional parameters cant be readen and your adress stays "". Just a guess though.

Thanks for your answer!

Which function do you mean? If you mean the constructor, the both Serial.println() calls work properly, so I thought the property address would be correctly stored in the object (but it isn't)...?

EDIT: I just found the (quite stupid) error: currentRequestsBatchIndex++;

increments this variable with every instantiation, so not only if I instantiate an object by purpose, but also when instantiating *requestsBatch[5];.

So replacing the problematic line by

      if(address != "") {
        currentRequestsBatchIndex++;
      }

this error is fixed.