Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I need help with my simple debug class.  (Read 1874 times)

0 Members and 1 Guest are viewing this topic.

MarbleXeno

  • Guest
I need help with my simple debug class.
« on: December 28, 2021, 02:11:12 pm »
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/6eU3hdY

So 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!
« Last Edit: December 28, 2021, 02:14:11 pm by MarbleXeno »

kojack

  • Full Member
  • ***
  • Posts: 170
    • View Profile
Re: I need help with my simple debug class.
« Reply #1 on: December 28, 2021, 02:42:08 pm »
Delete can only be called on things created with the new keyword.
From the way you call the AddDebugText function, it looks like testText is a regular variable (since it's being passed as a reference, not a pointer), such as a local variable or class member, which means it's most likely going on the stack and being cleaned up automatically. Delete can't be called on things like that.

As a simple example, this is incorrect:
sf::Text text;  // Make a text object on the stack.
sf::Text *p = &text; // Get the address of it.
delete p; // Try to delete it, but stack objects can't be deleted.
While this would work:
sf::Text *text = new sf::Text(); // Make a text object on the heap.
delete text; // New and delete work in pairs, text was allocated with new so it is cleaned up with delete.

MarbleXeno

  • Guest
Re: I need help with my simple debug class.
« Reply #2 on: December 28, 2021, 02:52:05 pm »
So there's no need to actually delete that pointer in the map? There will be no memory leak or something like that?

EDIT: Yes, testText was a regular variable.
EDIT2: Ahh okay, that makes sense! Now that i think about it, it really makes sense. Also, i don't think that Debug class should worry about deleting dynamically allocated sf::Text viariables, so that's why i'll create title manager so i have one place to actually manage it. Thanks a lot!
« Last Edit: December 28, 2021, 03:06:07 pm by MarbleXeno »