views:

374

answers:

4

I have a vector, defined by std::vector<LPDIRECT3DTEXTURE9> textures; Later, I am passing a LPDIRECT3DTEXTURE9 object to it, like so textures.push_back(texture); Here is a sample of this:

void SpriteManager::AddSprite(float x, float y, float z, LPDIRECT3DTEXTURE9 texture)
{
    //snip
    textures.push_back(texture);
    //snip
}

This is causing a runtime error. It is breaking in the vector class at the size() function. Why might this happen?

Edit:

I also run into an identical problem performing the same operation on a vector of D3DXVECTOR3 objects. Since LPDIRECT3DTEXTURE9 is a pointer to an IDIRECT3DTEXTURE9, should I be using that instead?

+1  A: 

Well, since LPDIRECT3DTEXTURE9 by its Hungarian name is a pointer and not an object (as you refer to it), my guess is that you are passing around invalid pointers which have already done a fandango on your poor vector object before you call push_back().

I might be wrong, but this is as much as can be said from the information you provide. And, yes, push_back() should only be able to fail if you are out of memory or trying to use a non-copyable or non-assignable object in the vector, and then not through an access violation.

Pontus Gagge
a vector of pointers does not care that the pointers it holds are invalid.
Bahbar
Nope. But if pointers and objects are confused, I'd start looking for invalid pointers somewhere around, as you also are suggesting.
Pontus Gagge
This is interesting. However, nothing happens to the pointer beforehand. I create the texture pointer, which I know is valid, and pass it to the function to use .push_back(). Nothing else happens to it along the way.
4501
I doubt this is the answer whether the pointer is invalid or not will not cause an access violation as push_back does not write through to the item being stored.
zebrabox
Nevertheless, it seems you are mangling the vector somewhere, whether through the stored pointers or something else. I just got suspicious when you called a pointer an object... Try changing the location of the textures vector (a member of SpriteManager?) by e.g. adding dummy char[512] data structures around it and see whether you get the same error in the same place.
Pontus Gagge
-1, this is a Windows environment from the name. Hence, we know that `std::vector<T*>` doesn't care about bad T*'s.
MSalters
+1  A: 

Your vector has been corrupted. I'd suggest putting a data watchpoint on its internals to see what is stomping on it (in a debugger).

Bahbar
+1  A: 

By far the most common reason is that you actually don't have a vector. In this case, textures appears to be a member of the SpriteManager class. So, that suggest you actually don't have a SpriteManager object either. Is the this pointer valid?

MSalters
A: 

I had a similar problem a while back: unexplained 'access violation' error when calling std::vector::push_back(). I solved it by cleaning the Visual Studio solution and rebuild! This obviously might not be the case here; however it could be as simple as that.

jack