2 vectors with pointers, one having negative size and capacity?

Hi folks.

I'm on a project for college where I'm creating a 2D platform game using SDL. My knowledge of C++ is quite limited thou and I have run into something I cannot figure out.

I have 2 vectors in my PlayState class std::vector<PlatformObject*> mpPlatformObjects and std::vector<NPCObject*> mpNPCObjects. Both PlatformObject and NPCObject are subclasses of GameObject.

When I initialize my PlayState class, I read data from several files so I can load all of my objects. This works fine for the first vector, all of my objects are being made, pushed back into the vector etc.

When trying to do the same for my second vector however, there seems to be a major problem.

Here's the code from my function I use to try and fill the vector.
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
//The npc offsets, min and max offsets, tiletype, health and damage
    int x = 0, y = 0, minX = 0, maxX = 0, t = -1, h = 0, dmg = 0;

	NPCObject* temp = NULL;

	//Open the map
    std::ifstream map((LEVEL_FOLDER + "npc1.map"));

    //Initialize the tiles
	while(map >> x){
		map >> y;
		map >> minX;
		map >> maxX;
		map >> t;
		map >> h;
		map >> dmg;

        //If the number is a valid tile number
        if((t >= 0) && (t < NPC_SPRITES)){
			temp = new NPCObject();
			temp->Load(x, y, minX, maxX, t, h, dmg);
			mpNPCObjects.push_back(temp);
        }
    }

    //Close the file
    map.close();


I tried debugging (using Visual Studio 2010 btw) and this error gets thrown "Unhandled exception at 0x000007fefd3bcacd in Game.exe: Microsoft C++ exception: std::length_error at memory location 0x001ff740.."

VS goes to vector and points at the line calling _Xlen();
1
2
3
4
5
6
7
8
9
10
	void _Reserve(size_type _Count)
		{	// ensure room for _Count new elements, grow exponentially
		size_type _Size = size();
		if (max_size() - _Count < _Size)
			_Xlen();
		else if ((_Size += _Count) <= capacity())
			;
		else
			reserve(_Grow_to(_Size));
		}


In my Autos tab I get
		_Count	5352818544	unsigned __int64
-		this	0x00000000001ff7f0 [-669102410](...)	std::vector<NPCObject *,std::allocator<NPCObject *> > * const
		[size]	-669102410	long
		[capacity]	-17	long


When I go and check what the size of my vector is, VS says my vector contains more than 1000000 child objects. Both size and capacity are 2146500608? Every object has "CXX0030: Error: expression cannot be evaluated"

What I'm guessing is that the vector tries to increase its size and this would be too much, giving the exception. Which is really weird, since it works fine for my first vector.

Anyone got any leads on this?

Cheers.
You're likely corrupting memory elsewhere (or possibly calling the function for an invalid instance).
Running the program with valgrind will tell you where the problem is.
Got it. Seems it was something related to the creation of the images for my objects. Guess I will have to implement some extra checks.

Thanks for the help, it actually made me realize to look further than just the vector and function itself.
Last edited on
Topic archived. No new replies allowed.