1
General / [SOLVED] sf::Text application crash
« on: June 18, 2020, 05:07:19 pm »
Hi,
Little help please...
The following simple example shows a reproducible crash with no compiler warnings.
1/ Example.cpp
2/ MyClass.h
3/ MyClass.cpp
Environment is VS 16.6.2 (latest), x64, SFML 2.5.1, Preprocessor SFML_STATIC, additional dependencies sfml-main.lib;sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;opengl32.lib;winmm.lib;freetype.lib;
I'm a bit fuzzy why this crashes... can you help?
Thanks!
Little help please...
The following simple example shows a reproducible crash with no compiler warnings.
1/ Example.cpp
#include <SFML/Graphics.hpp>
#include "MyClass.h"
int main()
{
// create a graphical text
sf::Font font;
font.loadFromFile("c:\\windows\\fonts\\arial.ttf");
sf::Text txt("sample text", font, 24);
// create a MyClass object
MyClass myObject;
sf::RenderWindow window(sf::VideoMode(800, 600), "");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(txt); // this works fine (if we comment out the next line)
window.draw(myObject.objectText); // this crashes application!
window.display();
}
}
#include "MyClass.h"
int main()
{
// create a graphical text
sf::Font font;
font.loadFromFile("c:\\windows\\fonts\\arial.ttf");
sf::Text txt("sample text", font, 24);
// create a MyClass object
MyClass myObject;
sf::RenderWindow window(sf::VideoMode(800, 600), "");
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(txt); // this works fine (if we comment out the next line)
window.draw(myObject.objectText); // this crashes application!
window.display();
}
}
2/ MyClass.h
#pragma once
#include <SFML/Graphics.hpp>
class MyClass
{
public:
MyClass();
sf::Text objectText;
};
#include <SFML/Graphics.hpp>
class MyClass
{
public:
MyClass();
sf::Text objectText;
};
3/ MyClass.cpp
#include "MyClass.h"
MyClass::MyClass()
{
sf::Font font;
font.loadFromFile("c:\\windows\\fonts\\arial.ttf");
objectText.setFont(font);
objectText.setString("object text");
objectText.setCharacterSize(24);
objectText.setPosition(25, 50);
}
MyClass::MyClass()
{
sf::Font font;
font.loadFromFile("c:\\windows\\fonts\\arial.ttf");
objectText.setFont(font);
objectText.setString("object text");
objectText.setCharacterSize(24);
objectText.setPosition(25, 50);
}
Environment is VS 16.6.2 (latest), x64, SFML 2.5.1, Preprocessor SFML_STATIC, additional dependencies sfml-main.lib;sfml-graphics-s.lib;sfml-window-s.lib;sfml-system-s.lib;opengl32.lib;winmm.lib;freetype.lib;
I'm a bit fuzzy why this crashes... can you help?
Thanks!