Skip to main content
Added comment as to which line needs to be removed for the first version to work.
Source Link

Why does static member std::map crashreset ESP8266 when outside the .ino file?

std::map<int, Test const *> Test::thing;

int Test::data() const 
{
    return this->_data;
}

void Test::_Reserve(Test const * const t) {
    Test::thing[t->data()] = t; // Works fine if this line is removed.
}

Test::Test(int x): _data(x) 
{
    Test::_Reserve(this);
}

Test::~Test() {};

Why does static member std::map crash ESP8266 when outside the .ino file?

std::map<int, Test const *> Test::thing;

int Test::data() const 
{
    return this->_data;
}

void Test::_Reserve(Test const * const t) {
    Test::thing[t->data()] = t;
}

Test::Test(int x): _data(x) 
{
    Test::_Reserve(this);
}

Test::~Test() {};

Why does static member std::map reset ESP8266 when outside the .ino file?

std::map<int, Test const *> Test::thing;

int Test::data() const 
{
    return this->_data;
}

void Test::_Reserve(Test const * const t) {
    Test::thing[t->data()] = t; // Works fine if this line is removed.
}

Test::Test(int x): _data(x) 
{
    Test::_Reserve(this);
}

Test::~Test() {};
Source Link

Why does static member std::map crash ESP8266 when outside the .ino file?

Here's an example of a class which crashes the ESP8266-03 causing it to reset:

Test.hpp file:

#include <map>

class Test {
public:
    int data() const;

    Test(int x);
    ~Test();
protected:
    int _data;

    static std::map<int, Test const *>thing;

    static void _Reserve(Test const * const t);
};

Test.cpp file:

std::map<int, Test const *> Test::thing;

int Test::data() const 
{
    return this->_data;
}

void Test::_Reserve(Test const * const t) {
    Test::thing[t->data()] = t;
}

Test::Test(int x): _data(x) 
{
    Test::_Reserve(this);
}

Test::~Test() {};

Project.ino:

#include "ESP8266WiFi.h"
#include "Test.hpp"

Test test(2);

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.print("Timer: ");
    Serial.println(millis());
    delay(1000);
}

The following does NOT crash the ESP8266-03 and prints the time as it should:

#include "ESP8266WiFi.h"
#include <map>

class Test {
public:
    int data() const 
    {
        return this->_data;
    }

    Test(int x): _data(x) 
    {
        Test::_Reserve(this);
    }
    ~Test() {};
protected:
    int _data;

    static std::map<int, Test const *>thing;

    static bool _Reserve(Test const * const t) {
        Test::thing[t->data()] = t;
    }
};

std::map<int, Test const *> Test::thing;

Test test(2);

void setup() {
    Serial.begin(115200);
}

void loop() {
    Serial.print("Timer: ");
    Serial.println(millis());
    delay(1000);
}

When the first version is uploaded to the board, I get the following in the serial monitor:

 ets Jan  8 2013,rst cause:1, boot mode:(1,7)


 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

When I reset it and boot it regularly, no upload mode, the following repeats every ~1 second:

 ets Jan  8 2013,rst cause:1, boot mode:(3,7)

load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v09f0c112
~ld