Reputation:
Trying to learn to code using Jim Horton's Beginning C++ Programming with Jim Horton. It's crashing with the following:
ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::_Find_lower_bound<std::string>(const std::string & _Keyval) Line 1704 C++
ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::_Find<std::string>(const std::string & _Keyval) Line 1457 C++
ZombieArena.exe!std::_Tree<std::_Tmap_traits<std::string,sf::Texture,std::less<std::string>,std::allocator<std::pair<std::string const ,sf::Texture>>,0>>::find(const std::string & _Keyval) Line 1466 C++
ZombieArena.exe!TextureHolder::GetTexture(const std::string & filename) Line 21 C++
ZombieArena.exe!Zombie::spawn(float startX, float startY, int type, int seed) Line 28 C++
The code for this is:
#pragma once
#ifndef TEXTURE_HOLDER_H
#define TEXTURE_HOLDER_H
#include <SFML/Graphics.hpp>
#include <map>
using namespace sf;
using namespace std;
class TextureHolder
{
private:
// A map container from the STL,
// that holds related pairs of String and Texture
map< string, Texture> m_Textures;
// A pointer of the same type as the class itself
// the one and only instance
static TextureHolder* m_s_Instance;
public:
TextureHolder();
static Texture& GetTexture(string const& filename);
};
#endif
The cpp file is:
#include "TextureHolder.h"
// Include the "assert feature"
#include <assert.h>
TextureHolder* TextureHolder::m_s_Instance = nullptr;
TextureHolder::TextureHolder()
{
assert(m_s_Instance == nullptr);
m_s_Instance = this;
}
Texture& TextureHolder::GetTexture(string const& filename)
{
// Get a reference to m_Textures using m_s_Instance
auto& m = m_s_Instance->m_Textures;
// auto is the equivalent of map<string, Texture>
// Create an iterator to hold a key-value-pair (kvp)
// and search for the required kvp
// using the passed in file name
auto keyValuePair = m.find(filename);
// auto is equivalent of map<string, Texture>::iterator
// Did we find a match?
if (keyValuePair != m.end())
{
// Yes
// Return the texture,
// the second part of the kvp, the texture
return keyValuePair->second;
}
else
{
// File name not found
// Create a new key value pair using the filename
auto& texture = m[filename];
// Load the texture from file in the usual way
texture.loadFromFile(filename);
// Return the texture to the calling code
return texture;
}
}
It crashes on the 2nd auto line for the map find. Any ideas???? I'm confused. Thanks in advance.
Upvotes: 0
Views: 2503
Reputation: 437
I also encountered a similar problem. It seems to be a BUG of the compiler. See here https://developercommunity.visualstudio.com/t/Exception-thrown:-read-access-violation/10666202?sort=newest&viewtype=solutions.
My solution is declare a static function in TextureHolder.cpp in which there is a static instance defined, and the function returns the instance.
TextureHolder& GetGlobalInstance()
{
static map< string, Texture> g_Textures;
return g_Textures;
}
Upvotes: 0
Reputation: 1
Add in the main:
// Here is the instance of TextureHolder
TextureHolder holder;
Upvotes: 0