Hello. I'm experiencing a bug in my code and I can't debug it.
Context: I've created a simple Debug class for debugging purposes. Later in the development i want to implement some kind of TitleManager in my AssetManager. Everything works fine but when it comes to destructor and deleting sf::Text pointers in my map it all crashes.
My std::map variable.
std::map<std::string, sf::Text*> m_textMap;
How i add values into it:
// in class
void Debug::AddDebugText(const std::string& _id, sf::Text& _debugText, const sf::Vector2f& _position)
{
m_textMap[_id] = &_debugText;
m_textMap[_id]->setPosition(_position);
}
And how I invoke it:
m_debug.AddDebugText("test", testText, { 0,0 });
My faulty destructor:
Debug::~Debug()
{
for (std::map<std::string, sf::Text*>::iterator itr = m_textMap.begin(); itr != m_textMap.end(); itr++)
{
delete (itr->second);
}
m_textMap.clear();
}
And error that I get when the destructor is called (when i close my window in this case):
https://imgur.com/a/6eU3hdYSo probably there's and error in my destructor or I screw something completely different.
I'm not that new into programming, but I didn't code in couple of months and I forgot many things, especially about how pointers operate in C++.
I know that there's probably a better way of doing this so I'm looking for ideas and advice of how I can handle it better or resolve the bug in my code. Thanks!