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

Author Topic: Crash after using sf::Text  (Read 5385 times)

0 Members and 1 Guest are viewing this topic.

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Crash after using sf::Text
« on: January 20, 2020, 05:18:32 pm »
Hello!

I have a trouble with using of sf::Text. If I use it in any class, or in main in my code it always crashes my app with error "Stack around the variable ... was corrupted".

What do you think about it?

Example code:
menu[0].setCharacterSize(40);
menu[0].setFillColor(sf::Color::Red);
menu[0].setString("Play!");
menu[0].setPosition(sf::Vector2f(400.f,PLAY*40.f));
 
« Last Edit: January 20, 2020, 05:44:17 pm by darvin55 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Crash after using sf::Text
« Reply #1 on: January 21, 2020, 02:35:19 pm »
Could you post a minimal working example showing your error? This way we can make sure it is not a coding mistake  :)

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #2 on: January 21, 2020, 04:17:01 pm »
MainMenu.h
#include "ConstState.h"
#include <SFML/Graphics.hpp>
class MainMenu : public ConstState
{
public:
        MainMenu();
        void draw(sf::RenderWindow& window);
private:
        sf::Text menu[3];
};
 

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

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

MainMenu::MainMenu()
{

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

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

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

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

main.cpp:
#include "MainMenu.h"
#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow window({ 800,600 }, "test");
        MainMenu menu;
        menu.draw(window);
        return 0;
}

 

Error:

Stack around the variable 'window' was corrupted.
« Last Edit: January 21, 2020, 04:45:59 pm by darvin55 »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Crash after using sf::Text
« Reply #3 on: January 21, 2020, 05:08:58 pm »
You should really be using a proper main-loop where you process events and continuously draw and display on your window, instead of drawing once and disposing everything right away.
Also, there's no font loaded for the text.

Can you provide a full call stack of the crash?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #4 on: January 21, 2020, 05:29:27 pm »
Okay, so I rewrite my code with tips you gave me, but now my problem is in sf::Font.

I can't load any font from file. I tried with many fonts and result is the same. I always have message in console "Failed to load font". In debugger I have info about 0xc0000005 in the line with loadFromFile() method. My Visual is in Polish so I think that crash description won't be understandable.

I'm sure that file with font is in project directory and in directory with executable file too.

My code after changes:

MainMenu.h:
#include <SFML/Graphics.hpp>
class MainMenu
{
public:
        MainMenu();
        void draw(sf::RenderWindow& window);
        void MoveUp();
        void MoveDown();
private:
        sf::Text menu[3];
        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 "MainMenu.h"
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
        sf::RenderWindow window({ 800,600 }, "test");
        MainMenu menu;
        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;
                                }
                                break;
                        case sf::Event::Closed:
                                        window.close();
                                        break;
                        }
                }
                window.clear();

                menu.draw(window);

                window.display();
        }
        return 0;
}
 
« Last Edit: January 21, 2020, 06:49:49 pm by darvin55 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Crash after using sf::Text
« Reply #5 on: January 21, 2020, 09:53:57 pm »
Maybe you are mixing debug/release binaries? Make sure you link against the *-d sfml libs for debug builds and the non post-fixed ones for release builds.

A general C++ advice : use std::array<sf::Text, 3> instead of a C-style array. Makes your life easier since you can use .size() etc. to make your code less bug prone.

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #6 on: January 22, 2020, 04:04:23 pm »
Okay, now in properties of project in Debug configuration i have only libs with *-d and in Release config - with without any sufixes. I also use std::array instead of classic arrays and i have now another problem.

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().

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: Crash after using sf::Text
« Reply #7 on: January 23, 2020, 11:06:00 am »
Probably you are accessing a variable through pointer/reference which is not "alive" anymore. Difficult to guess without seeing your new code.

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #8 on: January 25, 2020, 03:07:27 pm »
I don't change code too much

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;
}
 

MainMenu.cpp:
#include "MainMenu.h"
#include <iostream>

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

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

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

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

void MainMenu::draw(sf::RenderWindow& window)
{
        for (int i = 0; i < 3; i++)
        {
                window.draw(menu.at(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);
        }

}

unsigned int MainMenu::getCursor()
{
        return cursor;
}
 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Crash after using sf::Text
« Reply #9 on: January 25, 2020, 11:04:54 pm »
Where do you initialize / set the size of "menu" in menu.cpp before you try accessing its elements?

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #10 on: January 26, 2020, 10:56:21 am »
If you talk about main.cpp
MainMenu menu;

This line don’t initialize object? If no, how should I do it?

And if you talk about MainMenu.cpp, i have that line in header file:
std::array<sf::Text, 3> menu

darvin55

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Crash after using sf::Text
« Reply #11 on: January 27, 2020, 11:12:04 pm »
I've changed my code and now the problem is so weird...

I want to draw four sf::Text - crash.
I want to draw one sf::Text - it's ok.

Debugger shows error in line: window.draw(num2);

Debugger:


Actual code:

Settings.h:
#pragma once
#include "ConstState.h"
#include <SFML/Graphics.hpp>
#include <array>
class Settings
{
public:
        Settings();
        void runSettings(sf::RenderWindow& window, ConstState& state);
        void moveUp();
        void moveDown();
private:
        sf::Font* lucidra;
        sf::Text num1;
        sf::Text num2;
        sf::Text num3;
        sf::Text num4;
        void drawSettings(sf::RenderWindow& window);
        sf::Texture settingsT;
        sf::Sprite settingsS;
        int getCursor();
        std::array<sf::RectangleShape, 4> settings;
        int cursor;
        int countX;
        int countY;
        float ballV;
        float paddleV;
};

 

Settings.cpp (method I use):
void Settings::drawSettings(sf::RenderWindow& window)
{
        lucidra = new sf::Font;

        if (!(*lucidra).loadFromFile("res\\Lucida Fax Demibold.ttf"))
                std::cout << "error!!!!";

        num1.setFont(*lucidra);
        num1.setCharacterSize(40);
        num1.setFillColor(sf::Color::Color(0, 234, 235));
        num1.setString(std::to_string(countX));
        num1.setPosition(sf::Vector2f(320.f, 311.f));

        num2.setFont(*lucidra);
        num2.setCharacterSize(40);
        num2.setFillColor(sf::Color::Color(0, 234, 235));
        num2.setString(std::to_string(countY));
        num2.setPosition(sf::Vector2f(320.f, 374.f));

        num3.setFont(*lucidra);
        num3.setCharacterSize(40);
        num3.setFillColor(sf::Color::Color(0, 234, 235));
        num3.setString(std::to_string(ballV));
        num3.setPosition(sf::Vector2f(320.f, 448.f));

        num4.setFont(*lucidra);
        num4.setCharacterSize(40);
        num4.setFillColor(sf::Color::Color(0, 234, 235));
        num4.setString(std::to_string(paddleV));
        num4.setPosition(sf::Vector2f(320.f, 516.f));
       
        window.clear();
        window.draw(settingsS);
        window.draw(num1);
        window.draw(num2);
        window.draw(num3);
        window.draw(num4);

        for (int i=0;i<=3;i++)
        {
                window.draw(settings.at(i));
               
        }
}