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

Author Topic: My texture holder gets deleted although still in scope  (Read 271 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
My texture holder gets deleted although still in scope
« on: June 13, 2019, 04:37:30 pm »
So I'm new to SFML. I read a lot of post, but I really  don't get it.
I wrote an texture holder:
class tile_texture_holder {
   sf::Texture tx;
   tile_texture_holder(type a) {
      switch (a) {
      case type::desert:
      case type::grass:
      case type::mountain:
      case type::water:
   sf::Texture ret_texture() {
      return tx;

   ~tile_texture_holder() {
      std::cout << "///////////////////////HOLDER DELETED!!!/////////////////////" << std::endl;

And I tried to load a sprite with it in different ways....
For example:
tile_texture_holder t(type::desert);
      sf::Sprite s;
(in the same function, where I draw the sprite)
I always get the white box being drawn. And I really dont get why the texture_holder is getting deleted.
BTW type is an enum.

I hope somebody can help me solve my issue!


  • SFML Team
  • Hero Member
  • *****
  • Posts: 9353
    • View Profile
    • development blog
    • Email
Re: My texture holder gets deleted although still in scope
« Reply #1 on: June 13, 2019, 05:23:06 pm »
You create a new, local tile_texture_holder which creates a texture, then you set the texture to the sprite.
But since the tile_texture_holder is local, it will be destroyed as soon as you run out of scope, destroying the texture and thus the reference from the sprite to the texture becomes invalid.
Official FAQ: https://www.sfml-dev.org/faq.php
Nightly Builds: https://www.nightlybuilds.ch/
Dev Blog: https://dev.my-gate.net/
Thor: http://www.bromeon.ch/libraries/thor/