title: C++ Esp32 functional infinite loop
body: <p>So I've such of a code:</p>
  <pre><code>void B();
  void C();
  
  void A() {
    Serial.println(&quot;Looping&quot;);
    B();
  }
  
  void B() {
    C();
  }
  
  void C() {
    A();
  }
  
  
  void setup() {  
    Serial.begin(115200); 
    A();
  }
  
  void loop() {
   
  };
  </code></pre>
  <p>and it works just fine, but when I want to add a condition, like this:</p>
  <pre><code>bool flag = false;
  
  void B();
  void C();
  
  void A() {  
    Serial.println(&quot;Looping&quot;);
    if(flag)
      B();
    else 
      C();
  }
  
  void B() {
    flag = false;
    A();
  }
  
  void C() {
    flag = true;
    A();
  }
  
  
  void setup() {  
    Serial.begin(115200); 
    A();
  }
  
  void loop() {
   
  };
  </code></pre>
  <p>After a while my esp32 throws a backtrace and reboots:</p>
  <pre><code>Guru Meditation Error: Core  1 panic'ed (Unhandled debug exception)
  Debug exception reason: Stack canary watchpoint triggered
  </code></pre>
  <p>I need some kind of solution to run <code>B()</code> or <code>C()</code> and &quot;forget&quot; about <code>A()</code> body, as I understood this problem.</p>
  <p><strong>UDP</strong>
  It's just a skelet of a real code, I'm using FreeRTOS (xTaskCreate, ...) to run the logic,
  I`ve an idea:</p>
  <pre><code>1) Create 2 Tasks (one of them will be running) 
  2) Later pass the function I want to call, delete running
  3) Such way by switching tasks it should work
  </code></pre>
  <p>I've such logic in a project:</p>
  <pre><code>
  void B();
  void C();
  
  void A() {    
    if(server.connect())
      while(server.isConnected()) {
        //makeUpdate, receive new messages
      }
  
    //  as it fails, make reconnect
    //check wifi connection
      if(wifi)
        B();
      else 
        C();  
  }
  
  void B() {
    // Update Connection With Wifi
    // Go back to connect
    A();
  }
  
  void C() {
    // Update Connection With Gprs
    // Go back to connect
    A();
  }
  
  
  void setup() {  
    Serial.begin(115200); 
    A();
  }
  
  void loop() {
   
  };
  </code></pre>