Using cloud functions in constructor causes SOS

Not sure if this is common knowledge / known bug, caused me a bit of greif today :smile:

In my test case I called Udp.begin() in the constructor of a class that was being instanced from the main app, resulting in SOS.

Heres the minimal test case

buggy.h

#ifndef buggyh
#define buggyh

class buggy
{
    public:
        buggy();
};
#endif

buggy.cpp

#include <application.h>
#include "buggy.h"

buggy::buggy()
{
    Particle.publish("TEST"); //Udp.begin works too
}

bugtest.ino

#include "buggy.h"
buggy bug;

void setup() { }
void loop() {}

The solution, or mine atleast, is the move the network call into a function and call that during setup()

Hi @MORA

The order in which objects are constructed in C++ is not knowable, so your object constructor cannot rely on the Particle or the UDP objects to be created before your object. The compiler is free to do these in any order and that order can change from compile to compile if you add a new object.

The right way is to have a begin() method called in setup() that uses these other objects.

4 Likes