I'm running into an access violation in my resource manager class when it tries to declare a temporary sf::Texture object. I can't quite figure out why the error is occurring, although I suspect it may be related to the fact that I'm using an extern instance of the class so that it can be accessed by all other classes.
I created a simple project including only the resource manager class and a fairly basic main.cpp in order to be sure that this is where the problem is coming from, and sure enough, it is.
The offending line is at line 76 in ResourceManager.cpp:
sf::Texture temp;
resulting in an error message of:
Unhandled exception at 0x77242262 in FindingTheProblem.exe: 0xC0000005: Access violation writing location 0x00000004.
I think what's happening is, before the main function even begins to execute, the constructor of the ResourceManager is being called. It attempts to load in a default "error" image, texture, and sound for use when loading of a specified resource fails. The code to load each is identical except for what type of object it is creating and which map it is storing it in, and yet there are no problems with the image or the sound. Changing the order in which they are called also doesn't make a difference. It's only the texture that causes the violation. Why is simply declaring an sf::Texture object causing an access violation, and in that case, why do the sf::Image and sf::SoundBuffer not cause the same problem? Any ideas on how I can fix this?
For what it's worth, I'm using SFML 2.0-RC and C++, in Visual Studio 2010 running on Windows 7 (64-bit).
Here are the significant parts of my code. I'll attach the full code as well. I'm aware it's not the most efficient, and there are more features I plan to add to my resource manager, but I'm just trying to get it functioning in this basic state for now.
ResourceManager.h
class ResourceManager {
...
};
extern ResourceManager Resources; // Create a global instance of this class
#endif
ResourceManager.cpp
#include "ResourceManager.h"
ResourceManager Resources;
// Constructor
ResourceManager::ResourceManager()
{
//Load in the error-default data
loadImage("ERR_DEFAULT", "sprites/errdefault.png");
loadTexture("ERR_DEFAULT", "sprites/errdefault.png");
loadSound("ERR_DEFAULT", "sounds/errdefault.wav");
}
...
void ResourceManager::addTexture(const std::string& key, const sf::Texture& data)
{
textureBank.insert(std::make_pair(key, data));
}
...
bool ResourceManager::loadTexture(const std::string& key, const std::string& path)
{
//Ensure key is non-empty
if(key == "")
return false;
//Check whether key is already in bank. If it is, do not load data.
if(textureBank.find(key) != textureBank.end())
return false;
//Create a temporary texture object
sf::Texture temp;
//Ensure file loads successfully
if(!temp.loadFromFile(path))
return false;
//Add the data to the bank
addTexture(key, temp);
return true;
}
Thanks a lot for any help you can offer!
[attachment deleted by admin]