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

Author Topic: Trying to read the value of a sf::String causes crash  (Read 1061 times)

0 Members and 1 Guest are viewing this topic.

BiiXteR

  • Newbie
  • *
  • Posts: 20
    • View Profile
Trying to read the value of a sf::String causes crash
« on: August 16, 2016, 11:22:25 pm »
(Note : I'm still a beginner when it comes to programming, so sorry if it's something obvious)

So, when I try to print out the value of one of my function arguments of the type sf::String my entire application crashes with the error message ;

Quote
Exception thrown at 0x0FDE6D16 (msvcp140d.dll) in SwEngine.exe: 0xC0000005: Access violation reading location 0xCCCCCCCC.

If there is a handler for this exception, the program may be safely continued.

I believe  that a conversion between a std::String (which I send in to the function parameter of the type sf::String) fails, which leads to a crash, because when i try to use this sf::String value as a window title, the title is blank.

Here's some code :

This is the function which contains the sf::String value, and here's where the program crashes.

void Window::Create(unsigned int width, unsigned int height, sf::String title)
{

        std::cout << title.toAnsiString() << std::endl; // Crashes here.

}
 

And here's how I call that function :

Window::GetInstance().Create(1024, 720, globals::GAME_NAME);

And this is what the GAME_NAME variable is :

namespace globals
{

        // The name of the game the engine is currently running.
        const std::string GAME_NAME = "SwEngine Testing";

}
 

I'm pretty confused since it says in the SFML page that it handles conversions between the 2 string automatically, so, am I doing something very wrong here? :o

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
AW: Trying to read the value of a sf::String causes crash
« Reply #1 on: August 17, 2016, 12:20:36 am »
If you use globally initialized SFML resources (e.g. a render window) you will get crashes sooner than later. This is not how SFML should be used.

If you mix debug and release modes and libraries you will run into linker issues or if you bad luck crashes. Make sure to link debug libs in debug mode and release libs in release mode.

The same applies to x86 vs x64 libraries and compilers.

Without a minimal example (that you should provided) that reproduces the issue it's impossible to tell what the issue could be and we're left with guessing.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BiiXteR

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Trying to read the value of a sf::String causes crash
« Reply #2 on: August 17, 2016, 01:45:50 am »
Quote
This is not how SFML should be used.

Uhm, sorry again if this is a stupid question, but why should it not be used like that?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10819
    • View Profile
    • development blog
    • Email
Re: Trying to read the value of a sf::String causes crash
« Reply #3 on: August 17, 2016, 10:08:57 am »
Because the destruction of globally initialized variables is undefined, thus you get undefined behavior at destruction, because SFML is internally using globals as well.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

BiiXteR

  • Newbie
  • *
  • Posts: 20
    • View Profile
Re: Trying to read the value of a sf::String causes crash
« Reply #4 on: August 17, 2016, 01:49:36 pm »
Oh, alright. Thank you :P

 

anything