Why does static member std::map reset 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 &lt;map&gt;

class Test {
public:
    int data() const;

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

    static std::map&lt;int, Test const *&gt;thing;

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


Test.cpp file:

std::map&lt;int, Test const *&gt; Test::thing;

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

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

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 &lt;map&gt;

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

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

    static std::map&lt;int, Test const *&gt;thing;

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

std::map&lt;int, Test const *&gt; 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
Why does static member std::map reset ESP8266 when outside the .ino file?


What happens if the Test() constructor is run before the map constructor? This is the problem: you are assuming order of execution.

Use a Singleton pattern or init() member function to force the ordering that is required.

Cheers!