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

Author Topic: Memory problem with std::vector<sf::Sprite>  (Read 1785 times)

0 Members and 1 Guest are viewing this topic.

GreenyUk

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Memory problem with std::vector<sf::Sprite>
« on: July 05, 2015, 10:17:38 pm »
Hi all,

First post, new to SFML. Something is puzzling me about texture/sprite management and setup.

I'm managing my textures and sprites by loading all textures into a std::vector<sf::Texture>, and all sprites into a std::vector<sf::Sprite>.

The following code doesn't work:

1       this->tileTexures.push_back(sf::Texture());
2       this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
3
4       this->tileSprites.push_back(sf::Sprite());
5       this->tileSprites[0].setTexture(this->tileTexures[0]);
6
7       this->tileTexures.push_back(sf::Texture());
8       this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
9
10      this->tileSprites.push_back(sf::Sprite());
11      this->tileSprites[1].setTexture(this->tileTexures[1]);
 

When the code above runs, this->tileSprites[1] draws correctly, but this->tileSprites[0] has no texture.

I've followed this through with the debugger and observed the following:

Up to line 5 everything is fine. Once I call line 7, and call .push_back on the texture vector, the texture property of tileSprites[0] gets all messed up. Why is this?

If i change the order of this code however it works fine. The following code works a treat:

1       this->tileTexures.push_back(sf::Texture());
2       this->tileTexures.push_back(sf::Texture());
3
4       this->tileTexures[0].loadFromFile("../resources/spr_tile_1.png");
5       this->tileTexures[1].loadFromFile("../resources/spr_tile_2.png");
6
7       this->tileSprites.push_back(sf::Sprite());
8       this->tileSprites.push_back(sf::Sprite());
9
10      this->tileSprites[0].setTexture(this->tileTexures[0]);
11      this->tileSprites[1].setTexture(this->tileTexures[1]);
 

Same code, different order.

What's going on here? I've never run into this issue before where the order of my interactions with vectors seems to be altering memory somewhere.

Hopefully I've explained well enough, and thanks in advance for any input.

kitteh-warrior

  • Guest
Re: Memory problem with std::vector<sf::Sprite>
« Reply #1 on: July 05, 2015, 10:23:28 pm »
When you pass sf::Texture into a sf::Sprite, the sf::Sprite instance only contains a reference. When you increase the size of a std::vector<> past its allocation it will copy all of the contents to a new memory location, and remove the data at the previous location. This invalidates all references to the contents of the std::vector<> prior to it being resized, thus the sf::Sprite that had a reference to the first texture is invalid.

GreenyUk

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Memory problem with std::vector<sf::Sprite>
« Reply #2 on: July 05, 2015, 10:25:29 pm »
Ahh, so each time I add to a vector it moves the entire thing to a new memory location?

Is this standard practice for C++? I would have thought I'd have run into this before?!


Never-mind, I read the C++ reference and found it is C++ practice. Many thanks for the quick reply! Learned something new about vectors. :)
« Last Edit: July 05, 2015, 10:27:50 pm by GreenyUk »

kitteh-warrior

  • Guest
Re: Memory problem with std::vector<sf::Sprite>
« Reply #3 on: July 05, 2015, 10:26:29 pm »
Not each time, it is only when to amount of memory required for the new size exceeds that in which has already been allocated to.
See http://www.cplusplus.com/reference/vector/vector/resize/ for a reference.

GreenyUk

  • Newbie
  • *
  • Posts: 7
    • View Profile
    • Email
Re: Memory problem with std::vector<sf::Sprite>
« Reply #4 on: July 05, 2015, 10:28:26 pm »
Ahh, beat my edit.

Cheers, Kitteh-warrior. :)