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

Author Topic: Text/font causes crash on shutdown  (Read 2294 times)

0 Members and 1 Guest are viewing this topic.

JohanJ

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Text/font causes crash on shutdown
« on: January 06, 2019, 12:12:31 am »
I have started with the tutorials and let my project grow. When I added a text it started to crash at shutdown, it does not affect the gameplay but it is annoying. Now, I have shrunk the code to a minimal example. Can you see what is wrong?

#include <SFML/Graphics.hpp>
int main()
{
        sf::RenderWindow window(sf::VideoMode(700, 400), "hejsan", sf::Style::Default);
        sf::Font font_;
        sf::Text text_;
        // Just any font. (Still crashes at shutdown even if the line is removed.):
        font_.loadFromFile("C:\\Windows\\fonts\\DejaVuSansMono.ttf");
        text_.setFont(font_);
        text_.setString("hejsan");

        // run the program as long as the window is open
        while (window.isOpen())
        {
                // check all the window's events that were triggered since the last iteration of the loop
                sf::Event event;
                while (window.pollEvent(event))
                {
                        // "close requested" event: we close the window
                        if (event.type == sf::Event::Closed)
                                window.close();
                }
                window.draw(text_);

                window.display();
        }
        return 0;
}
 
Compiler (build chain):
Microsoft Visual C++ 2017
Error message:
Exception thrown at 0x00303A18 in ConsoleApplication1.exe: 0xC0000005: Access violation writing location 0xCDCDCDCD
Call stack:
   ConsoleApplication1.exe!std::_Container_base12::_Orphan_all() Line 256   C++   Non-user code. Symbols loaded.    ConsoleApplication1.exe!std::_Vector_alloc<std::_Vec_base_types<sf::Vertex,std::allocator<sf::Vertex> > >::_Orphan_all() Line 580   C++   Non-user code. Symbols loaded.
    ConsoleApplication1.exe!std::vector<sf::Vertex,std::allocator<sf::Vertex> >::_Tidy() Line 2003   C++   Non-user code. Symbols loaded.
    ConsoleApplication1.exe!std::vector<sf::Vertex,std::allocator<sf::Vertex> >::~vector<sf::Vertex,std::allocator<sf::Vertex> >() Line 921   C++   Non-user code. Symbols loaded.
    ConsoleApplication1.exe!sf::VertexArray::~VertexArray()   C++   Non-user code. Symbols loaded.
    ConsoleApplication1.exe!sf::Text::~Text()   C++   Non-user code. Symbols loaded.
>   ConsoleApplication1.exe!main() Line 27   C++   Symbols loaded.
    [External Code]      Annotated Frame


FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #1 on: January 06, 2019, 12:31:33 am »
This code is correct (other than lack of clear but that shouldn't crash). Are you sure you're not mixing debug and release and if not, where did you get your libraries from?
Back to C++ gamedev with SFML in May 2023

JohanJ

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #2 on: January 06, 2019, 01:36:18 am »
On a closer inspection I see that I link with 2.4.2 library of unknown origin. I had downloaded  a more recent version from the download site but apparently I had failed to make the correct settings in the IDE  :-[

Hm, it seems as if I have to upgrade Visual Studio if I want to use the latest lib:

https://en.sfml-dev.org/forums/index.php?topic=24044.msg163201#msg163201

Thank you for the swift answer! I must get my environment in order. It was probably a bad lib  :)

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #3 on: January 06, 2019, 01:53:43 am »
Quote
I had downloaded  a more recent version from the download site but apparently I had failed to make the correct settings in the IDE  :-[
If you link dynamically then remember to copy fresh dlls near your exe too.

I'm actually thinking of what exactly might be happening (like which pointer exactly it tries to read that is set to 0xCDCDCDCD and what is in that pointers place for real). 0xCDCDCDCD is one of the debug patterns BTW: https://stackoverflow.com/a/8275454

Quote
Hm, it seems as if I have to upgrade Visual Studio if I want to use the latest lib:
Linking error is more clear though. Is your VS not up to date for any reason? It keeps bugging you with a yellow flag icon 'notification' to upgrade on launch, no (I keep mine up to date due to that)?

You can get also build SFML yourself. All you need is copy of the code (from website or cloned with git or downloaded in zip from GitHub), CMake and VS. It's not hard at all and then you're sure it'll work.
Back to C++ gamedev with SFML in May 2023

JohanJ

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #4 on: January 06, 2019, 05:38:18 pm »
Thank you for help and suggestions! My environment was not in order, it can be that I used 2.5 header files and 2.4 libs even :-[

I will take a look at CMake. I really like the VC++ debugger but I find its configuration/project/solution handling really cumbersome. I always find myself spending too much time searching among strange menu choices when I work with VC++, even though I should have spent more time there this time  :)

again, thanks it work now 8)

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #5 on: January 06, 2019, 06:34:14 pm »
There is no need to spend time in any strange menus with CMake and VS at all.

It's very easy, quick and there is a good tutorial on what to do exactly: https://www.sfml-dev.org/tutorials/2.5/compile-with-cmake.php

It'll also help you to know how to build using CMake once you want to use some library that doesn't have precompiled libraries and just has code and CMake file.
« Last Edit: January 06, 2019, 06:39:26 pm by FRex »
Back to C++ gamedev with SFML in May 2023

JohanJ

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #6 on: January 08, 2019, 07:05:23 pm »
Good, seems as if I will be able to do that!

Is there a way to let CMake create a new VS project and automatically generate the result of the steps in the "SFML and Visual studio" tutorial https://www.sfml-dev.org/tutorials/2.5/start-vc.php ?

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Text/font causes crash on shutdown
« Reply #7 on: January 08, 2019, 10:16:43 pm »
CMake as a tool yes but you'd need to write your own files. The CMake files included with SFML are for generating makefiles/projects/etc. for building SFML itself. Maybe some files that generate projects to build examples would be a good start. I don't know how to write CMake files. I should learn it but I keep putting it off. You could try use VS project templates too. I never did that either since it'd save like 1-2 minutes per project at most so I didn't bother.
Back to C++ gamedev with SFML in May 2023