Crash when using arrays of sf::Text

0

When I load and set font, app crashes and debugger shows me 0xc0000005 at line in MainMenu.cpp (exception is reported by sfml-graphics-d-2.dll):

window.draw(menu.at[i]);

This is line in for loop.

Btw. every time I close my app I have an error "Run-Time Check Failure #2 - Stack around the variable 'window' was corrupted." and debugger show end of main().

MainMenu.h:

#include <SFML/Graphics.hpp>
class MainMenu
{
public:
        MainMenu();
        void draw(sf::RenderWindow& window);
        void MoveUp();
        void MoveDown();
private:
        std::array<sf::Text,3> menu;
        sf::Font arial;
        unsigned int cursor{ 0 };
};

MainMenu.cpp:

#include "MainMenu.h"
#include <SFML/Graphics.hpp>
#include <iostream>

constexpr int PLAY{ 1 };
constexpr int SETTINGS{ 2 };
constexpr int EXIT{ 3 };

MainMenu::MainMenu()
{
        arial.loadFromFile("ArialUnicodeMS.ttf");

        menu[0].setFont(arial);
        menu[0].setCharacterSize(40);
        menu[0].setFillColor(sf::Color::White);
        menu[0].setString("Play!");
        menu[0].setPosition(sf::Vector2f(400, PLAY * 40));

        menu[1].setFont(arial);
        menu[1].setCharacterSize(40);
        menu[1].setFillColor(sf::Color::White);
        menu[1].setString("Settings");
        menu[1].setPosition(sf::Vector2f(400, SETTINGS * 40));

        menu[2].setFont(arial);
        menu[2].setCharacterSize(40);
        menu[2].setFillColor(sf::Color::White);
        menu[2].setString("Exit");
        menu[2].setPosition(sf::Vector2f(400, EXIT * 40));
}

void MainMenu::draw(sf::RenderWindow& window)
{
        for (int i = 0; i < 3; i++)
        {
                window.draw(menu[i]);
        }
}

void MainMenu::MoveUp()
{
        if (cursor - 1 >= 0)
        {
                std::cout << "moved up!";
                menu[cursor].setFillColor(sf::Color::White);
                cursor--;
                menu[cursor].setFillColor(sf::Color::Red);
        }

}

void MainMenu::MoveDown()
{
        if (cursor + 1 < 3)
        {
                std::cout << "moved down!";
                menu[cursor].setFillColor(sf::Color::White);
                cursor++;
                menu[cursor].setFillColor(sf::Color::Red);
        }

}

main.cpp:

#include "Game.h"
#include "MainMenu.h"
#include "ConstState.h"
#include "Settings.h"
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
        sf::RenderWindow window(sf::VideoMode(800,600), "test");
        Game game;
        MainMenu menu;
        Settings settings;
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::KeyReleased:
                                switch (event.key.code)
                                {
                                case sf::Keyboard::Up:
                                        menu.moveUp();
                                        break;
                                case sf::Keyboard::Down:
                                        menu.moveDown();
                                        break;
                                case sf::Keyboard::Return:
                                        switch (menu.getCursor())
                                        {
                                        case 0:
                                                std::cout << "play";
                                                game.run(window);
                                                break;
                                        case 1:
                                                std::cout << "settings";
                                                break;
                                        case 2:
                                                window.close();
                                                break;
                                        }
                                        break;
                                }
                                break;
                                case sf::Event::Closed:
                                        window.close();
                                        break;

                        }
                }
                window.clear(sf::Color::Red);

                menu.draw(window);

                window.display();
        }
        return 0;
}
c++
sfml
asked on Stack Overflow Jan 26, 2020 by LeTeeL

1 Answer

0

In MainMenu::MoveUp() you write: if (cursor - 1 >= 0) ...

First of all, this condition is always true: cursor is an unsigned int, so it has no other options but to be >= 0.

Secondly, in the case when cursor is 0, your cursor - 1 yields the maximum value of unsigned int (as it wraps on overflow). As a result, menu[cursor].setFillColor... causes undefined behaviour because you give your std::array<sf::Text, 3> menu an invalid index.

Try fixing this and check if the error shows up again.

answered on Stack Overflow Feb 6, 2020 by passing_through

User contributions licensed under CC BY-SA 3.0