I have a program with a main function that simply prints a string. When I run this program it crashed without output in the console. I found out the problem happens when I insert an element into the map of OperatorCore (symbolMap).
This is the minimal code:
//Binary.hpp
class Binary final : public OperatorCore, public StaticPool<Binary> {
public:
Binary(int ID, std::string name)
: OperatorCore(name), StaticPool<Binary>(ID) {
}
~Binary() {}
};
//Binary.cpp
template<>
const Binary StaticPool<Binary>::pool[] = {
Binary(0, "a string value")//without this line of code, it prints works
};
//OperatorCore.hpp
class OperatorCore {
public:
static std::map<std::string, OperatorCore*> symbolMap;
const std::string name;
OperatorCore (std::string name);
virtual ~OperatorCore () {}
};
//OperatorCore.cpp
std::map<std::string, OperatorCore*> OperatorCore::symbolMap{};
OperatorCore::OperatorCore(std::string name) : name(name) {
symbolMap.insert({name, this});
}
//StaticPool
template<typename T, typename TKey = int>
class StaticPool {
public:
const TKey ID;
static const T pool[];
StaticPool(TKey ID) : ID(ID) {}
virtual ~StaticPool() {}
};
The problem does not occur if I delete one of the highlighted lines. Does this design cause a memory corruption?
EDIT: The initialization of OperatorCore::symbolMap is in the same file where is also the implementation of OperatorCore constructor.