sfml texture shared_ptr error

Hi
Im using sfml and im trying to load a texture


Ive made a map of strings and textures, m_textures:
std::map<std::string, std::shared_ptr<sf::Texture>> m_textures;

and im loading textures in this function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
std::shared_ptr<sf::Texture> MyAssetManager::LoadTexture(std::string file)
{


   std::shared_ptr<sf::Texture> tex = std::make_shared<sf::Texture>();

   if(!tex->loadFromFile(file))
      return nullptr;


   m_textures[file] = tex;

   return tex;
}


The probram builds fine but gives me an error at runtime.


You don't provide declarations for the things you use or show how the return from this function is used, leaving us to guess what you intend and what you've done.

Having said that, shouldn't you return the empty texture rather than null?
If you later try to use the subscript operator to access a texture in the map you need to make sure the file key is correct otherwise you'll get a nullptr back.
Last edited on
Thanks or the responses,
All im doing is creating an MyAssetManager object and calling the LoadTexture function.
(Im not assigning anything to it)

When i parse a filepath that doesnt exist, sfml just logs out to the console that the file couldnt be found.

Its when i parse a valid filepath the program crashes
Last edited on
And it crashes inside this function? Which line?
seems to be on the line where i assign the texture to the map (line 11)

when i remove that, it works fine

EDIT:
i tried making a map of <int, int> and assigned a key a value
but that crashes too?

pretty wired
Last edited on
If the MyAssetManager object is static/global are you sure that it has been created before LoadTexture is called?

It's really hard to guess because the true cause of the problem seems to be elsewhere. You might want to put together a small example that can be compiled and contains the minimum code necessary to demonstrate the problem. By doing so you are likely to find the the problem yourself, but otherwise you can post it here and we will probably be able to help you.
Registered users can post here. Sign in or register to post.