I've got this piece of code:
class BuffComponent : public Component
{
public:
using BuffMap =
std::unordered_map<Effect, std::unique_ptr<EffectComponent>>;
BuffMap buffs;
BuffComponent() {}
BuffComponent(const BuffComponent &buff_component)
{
for (auto &buff : buff_component.buffs)
{
this->buffs[buff.first] =
std::unique_ptr<EffectComponent>(buff.second->clone());
}
}
BuffComponent(std::initializer_list<EffectComponent *> buffs_list)
{
for (auto &buff : buffs_list)
{
buffs[(buff->effect & ~(APPLIED | APPLY_ONCE))] =
std::unique_ptr<EffectComponent>(buff);
}
}
BuffComponent *clone() const override { return new BuffComponent(*this); }
};
Which compiles perfectly fine. My main looks like this (for testing)
int main()
{
std::cout << "I HAVEN'T CRASHED!";
std::unique_ptr<BuffComponent> i(new BuffComponent());
}
The base Component class looks like this:
class Component
{
public:
virtual Component *clone() const = 0;
virtual ~Component() {}
};
The problem I'm experiencing is that for whatever reason, the program crashes and doesn't display the "I HAVEN'T CRASHED!"
. I can't even seem to debug it with gdb, as it immediately quits with an exit code 139 (which I found has something to do with memory). My question is, what is wrong about my understanding? I can't see anything obvious that would cause this to compile but fail at runtime.
When the lines responsible for map acccess are commented inside the for each loop, the program does not crash and functions correctly. This leads me to believe that accessing the map violates the memory somehow, but I don't understand why. I'd really appreciate any help, it's been driving me nuts