SFML community forums

Help => Graphics => Topic started by: 51423benam on December 28, 2017, 01:51:45 pm

Title: sf::Text problem with a shared_ptr
Post by: 51423benam on December 28, 2017, 01:51:45 pm
Hello,

I have a shared_ptr to a sf::Text object, declaration:
    std::shared_ptr<sf::Text> title;

But when I want for example to set the character size like so:
    title->setCharacterSize(24);

... a exception is thrown:
    "this" was "Nullptr"   (in Visual Studio 2017)

But, strangely, when I set the font before that, like so:
    title->setFont(titleFont);

... the exception still appears at setCharacterSize, setFont works fine!

Do you know why this could happen?
Title: Re: sf::Text problem with a shared_ptr
Post by: eXpl0it3r on December 28, 2017, 02:27:13 pm
Have you initialized the text object? If not then accessing a nullptr is undefined behavior.
Use std::make_shared to create an instance.
Also you most likely want to create the object on the stack or at least use unique ownership with unique_ptr.
Title: Re: sf::Text problem with a shared_ptr
Post by: 51423benam on December 28, 2017, 10:03:07 pm
Oops, I just forgot make_shared  :o, thanks!