Hello all,
I made a little SpriteManager class which will handle sprites (and textures) loading and passing around. This is my add function:
signed int Phox::cSpriteManager::Add(const std::string& Path, bool Smooth)
{
for (unsigned int i = 0; i < Paths.size(); i++)
if (Paths[i] == Path) return -1;
sf::Texture* Texture = new (std::nothrow) sf::Texture;
if (!Texture) return -2;
sf::Sprite* Sprite = new (std::nothrow) sf::Sprite;
if (!Sprite) return -3;
if (!Texture->loadFromFile(Path))
{
delete Texture;
delete Sprite;
return -4;
}
Texture->setSmooth(Smooth);
Sprite->setTexture(*Texture);
Paths.push_back(Path);
Textures.push_back(Texture);
Sprites.push_back(Sprite);
return Sprites.size() - 1;
}
And here is the destructor:
Phox::cSpriteManager::~cSpriteManager()
{
std::cout << "SpriteManager: Calling Dtor.\n";
for (unsigned int i = 1; i < Sprites.size(); i++)
if (Sprites[i] != 0) delete Sprites[i];
//for (unsigned int i = 1; i < Textures.size(); i++)
//if (Textures[i] != 0) delete Textures[i];
Textures.clear();
Sprites.clear();
Paths.clear();
std::cout << "SpriteManager: Finished Dtor.\n";
}
Occasionally when main() ends and the above lines are not commented I get a 0xC0000005 segfault.. However commenting the lines never yields the error. The weird part about the error is that when it crashes I see both "Calling Dtor." and "Finished Dtor." which makes me believe that deleting the textures doesn't actually cause the segfault. Like I said though, commenting those two lines never ever crashes.
I'm using the latest SFML github source (2 days ago) and GCC 4.7.0. Any help in understanding the problem would be really appreciated.