SFML community forums

Help => Graphics => Topic started by: RadWayne on January 10, 2014, 11:04:06 pm

Title: Creating a Vector of Shapes
Post by: RadWayne on January 10, 2014, 11:04:06 pm
I have no problem creating and using shapes stored in a vector but when I try and close the window while using a shape stored in a vector I "trigger a breakpoint".
The error is being thrown from free.c from a  function used to free a memory block in the heap.

What exactly is happening and why is there a problem freeing memory?

int main()
{
   
    sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
        sf::RectangleShape rect1(sf::Vector2f(100,50));
        rect1.setFillColor(sf::Color(255,0,0));
        std::vector <sf::RectangleShape> vectorRect(1);
        vectorRect[0]=rect1;

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

                window.draw(vectorRect[0]);
        window.display();
    }

    return 0;
}
Title: Re: Creating a Vector of Shapes
Post by: Assassin0795 on January 12, 2014, 03:08:34 am
Instead of just:

window.draw(vectorRect[0]);
window.display();

Try:

window.clear();
window.draw(vectorRect[0]);
window.display();

I had funky issues with my program because I forgot to add window.clear() once.
Title: Re: Creating a Vector of Shapes
Post by: RadWayne on January 12, 2014, 03:14:54 am
Nope.

It has something to do with SFML trying to free memory allocated for the vector. Someone else suggested I compile SFML on my own so I'm going to try that.
Title: Re: Creating a Vector of Shapes
Post by: Omega on January 12, 2014, 05:25:53 am
It works fine for SFML 2.0. Are you using a different version?
Title: Re: Creating a Vector of Shapes
Post by: Nexus on January 12, 2014, 02:40:28 pm
From a logic point of view, you shouldn't be rendering anything when the window is closed. I think it's a good idea to avoid this situation, even if there were no technical problems with it.