I'm working on this very basic texture handler class using the SFML library. and the code looks like this:
// H
class TexturesHandler
{
friend class XML_Handler;
public:
...
static TexturesHandler &init() { return m_handler; };
sf::Texture &getTexture(std::string name);
...
private:
...
static TexturesHandler m_handler;
std::map<std::string, sf::Texture> m_Textures;
void setTexture(std::string path, std::string name, const sf::IntRect& rect = sf::IntRect());
...
};
// CPP
#include "TexturesHandler.h"
...
TexturesHandler TexturesHandler::m_handler;
sf::Texture &TexturesHandler::getTexture(std::string name)
{
return m_Textures[name];
}
//private
...
void TexturesHandler::setTexture(std::string& path, std::string& name, const sf::IntRect& rect)
{
sf::Texture texture;
texture.loadFromFile(path, rect);
m_Textures[name] = texture;
}
basically what this class does is load all textures at the beginning of the game
with the help of XML_Handler
which is another class that reads the name, path and size of every texture from a XML file,and any sprite that needs a texture can call getTexture
.
but the problem is that when the setTexture
function is called for the first time a runtime exception is thrown at the 3rd line of the function from the header file xtree
line 2046
with the message "read access violation _Wherenode was nullptr"
keep in mind that:
1- TextureHandler is a singlton class
2- by setting a break point inside the function setTexture
it seems that all arguments passed by XML_Handler are correct and non of them is set to 0 or null...
3-I tries using emplace and insert functions to insert but it doesn't seem to work
I never got any similar exception before and searching for a solution doesn't seem to yield any results either. so my question is:
1-why do I get this exception when trying to insert a texture in the map ?
(optional)2- what are all the stl containers that use xtree
as a base class ?
EDIT:
after a lot of testing it looks like the function setTexture
is being called before the constructor . I tried to place a break point in both of them and the one in the function is getting hit before the one in the constructor. how is that even possible ???
also I still get the runtime exception when I try to access the map. even if it was something as simple as m_Textures["bla"];
another very bizarre thing is that even though the XML_Handler
class is a friend it can access all data members and functions of the TextureHandler
class except for the constructor so this is valid : TexturesHandler::init().~TexturesHandler();
while this : TexturesHandler::init().TexturesHandler();
gives the error:
type name is not allowed