Error with String Constants

closed account (N36fSL3A)
I'm getting an error with string constants. It's an internal string error, it happens at runtime.

It claims I'm accessing memory I'm not supposed to. Here are the functions I'm getting the error with.

Strangely it started occurring once I switched from C strings to STL strings...

Not much else I have the knowledge to explain.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		int LoadResource(const std::string location, ResourceType type)
		{
			bool resLoaded = isLoaded(location, type);

			if(resLoaded) {return -1;} // Tells the resource was already loaded.

			if(type == R_TEXTURE)
			{
				// Load the texture
				Video::Texture texture(location.c_str());

				Resource<Video::Texture>* res = new Resource<Video::Texture>(texture, location);

				// Load the texture into a resource template, then to a shared_ptr object.
				std::shared_ptr<Resource<Video::Texture>> tmpSRes(res);

				// Add the shared_ptr to the texture database.
				textures.push_back(tmpSRes);
			}

			else if(type == R_MESH)
			{
				Mesh mesh;

				mesh.LoadOBJT(location.c_str(), std::shared_ptr<Resource<Video::Texture>>(nullptr));
			}

			// Now since a resource was loaded, we can safely add 1 to the numResources variable...
			numResources++;

			return 0;
		}

		bool isLoaded(const std::string location, ResourceType type) const
		{
			// Based on the type, we loop through our
			// resource data base and check if the texture is loaded.
			// (Look at each resource's HD location, and based on that we check if it matches the
			// resource we'd like to load's HD location. If it does, we return true)
			switch(type)
			{
				case R_TEXTURE:
					for(unsigned int r = 0; r < textures.size(); r++)
					{
						if(location == textures[r]->getID())
						{
							return true;
						}
					}
				break;

				case R_MESH:
					for(unsigned int r = 0; r < meshes.size(); r++)
					{
						if(location == meshes[r]->getID())
						{
							return true;
						}
					}
				break;

				case R_MODEL:
					for(unsigned int r = 0; r < models.size(); r++)
					{
						if(location == models[r]->getID())
						{
							return true;
						}
					}
				break;

				default:
					return false;
				break;
			}

			// If there wasn't a match
			return false;
		}
Hi. What is the error message? Do you know where the error is occurring? Can you post the code for the Resource class?
closed account (N36fSL3A)
The problem is happening in the conversion of c_strings to std::strings in this line where the function is being called:

bool resLoaded = isLoaded(location, type);

and externally from the file:

VC++ says it occurs in xstring.h and points to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
	_Myt& assign(const _Myt& _Right,
		size_type _Roff, size_type _Count)
		{	// assign _Right [_Roff, _Roff + _Count)
		if (_Right.size() < _Roff)
			_Xran();	// _Roff off end
		size_type _Num = _Right.size() - _Roff;
		if (_Count < _Num)
			_Num = _Count;	// trim _Num to size

		if (this == &_Right)
			erase((size_type)(_Roff + _Num)), erase(0, _Roff);	// substring
		else if (_Grow(_Num))
			{	// make room and assign new stuff
			_Traits::copy(_Myptr(), _Right._Myptr() + _Roff, _Num);
			_Eos(_Num);
			}
		return (*this);
		}



The resource class is fairly large, it took up about 2 posts to fit it alone when I posted an earlier version of it.
Last edited on
bool isLoaded(const std::string& location, ResourceType type) const
?
closed account (N36fSL3A)
I don't know where you're referencing to.
Last edited on
closed account (N36fSL3A)
Wow, it turned out to be a problem in the ResourceManager constructor :|
Duoas was refering to line 34, saying you should pass the sttring by reference
Topic archived. No new replies allowed.