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

Author Topic: Sprite Texture Reference Invalidated by vector.push_back  (Read 3866 times)

0 Members and 1 Guest are viewing this topic.

Azmandias

  • Newbie
  • *
  • Posts: 2
    • View Profile
Sprite Texture Reference Invalidated by vector.push_back
« on: July 20, 2014, 01:22:02 am »
Hi, y'all.
I've been trying to load a series of sprites into a vector via the following loop.
                sf::Sprite spritey;
                for (unsigned int i = 0, k = 0; i < tobesprited.size(); ++k) {
                                loadSprite(tobesprited.at(i), &spritey);
                                mdeckVector.push_back(thisCard);
                }
Where loadSprite is
void loadSprite(int textCode, sf::Sprite* spritey) {
                        //textMap is an unordered_map I keep textures in
auto checkExists = textMap.find(textCode);
if (checkExists == textMap.end()) {
                        sf::Texture     texturey;
                        char texName[64];
                        sprintf_s(textName, 64, "./pics/%i.jpg", textCode);
                        texturey.loadFromFile(textName);
                        //textMap is an unordered_map I keep textures in
                        textMap.insert(std::make_pair(textCode, texturey));
                        spritey->setTexture(texturey);
}
else
spritey->setTexture(checkExists->second);
}
The problem happens at
                                loadSprite(tobesprited.at(i), &spritey);
                                spriteVector.push_back(thisCard);
If I check the size of the texture spritey references after I load the sprite, it's exactly what it's supposed to be.  But if I check it after spriteVector.push_back, it'll be 3435973836 by 3435973836, except on the penultimate sprite in the vector.  Then, if I leave the window, I'll get a Divide by Zero exception from Texture.cpp
 // If pixels are flipped we must invert the Y axis
            if (texture->m_pixelsFlipped)
            {
                matrix[5] = -matrix[5];
//On the next line
                matrix[13] = static_cast<float>(texture->m_size.y / texture->m_actualSize.y);
            }
I've tried rewriting the vector to handle unique_ptr<sf::Sprite>'s, but it doesn't fix anything.  What am I doing wrong?

Compiled on VS2013, SFML Dynamic.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Texture Reference Invalidated by vector.push_back
« Reply #1 on: July 20, 2014, 02:10:01 am »
When you insert the texture into the map, you create a copy. The local object -- which you reference from the sprite -- is destroyed at the end of the scope.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Sprite Texture Reference Invalidated by vector.push_back
« Reply #2 on: July 20, 2014, 02:29:59 am »
Here's some inspiration for you on different ways to manage resources in containers:

My own Jukebox example on the wiki.

The resource classes from the excellent Thor library.

The ResourceHolder class from the SFML Game Development book.

Azmandias

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Sprite Texture Reference Invalidated by vector.push_back
« Reply #3 on: July 20, 2014, 07:17:56 am »
When you insert the texture into the map, you create a copy. The local object -- which you reference from the sprite -- is destroyed at the end of the scope.
That's what I thought, but what threw me off was the penultimate object in the vector being fine.
Out of a size object test, the fifth is the only one that is the right size after I push it into the vector, and the only one that I can get from the vector later that is the right size.  Why does it work for that one, but not the others?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Sprite Texture Reference Invalidated by vector.push_back
« Reply #4 on: July 20, 2014, 08:49:10 am »
Out of a size object test, the fifth is the only one that is the right size after I push it into the vector, and the only one that I can get from the vector later that is the right size.  Why does it work for that one, but not the others?
It does not work for that one, the object is dead as well. What you get is undefined behavior.

By the way, next time you post please consider the forum rules concerning minimal complete examples. It makes the issue immediately clear and doesn't lead to misunderstandings.
« Last Edit: July 20, 2014, 08:54:24 am by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: