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

Author Topic: sf::Text problem with a shared_ptr  (Read 3479 times)

0 Members and 1 Guest are viewing this topic.

51423benam

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
sf::Text problem with a shared_ptr
« 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: sf::Text problem with a shared_ptr
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

51423benam

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: sf::Text problem with a shared_ptr
« Reply #2 on: December 28, 2017, 10:03:07 pm »
Oops, I just forgot make_shared  :o, thanks!

 

anything