[英]Runtime error: Access violation when using .push_back() with a std::vector?
I have a vector, defined by std::vector<LPDIRECT3DTEXTURE9> textures;
我有一个矢量,由
std::vector<LPDIRECT3DTEXTURE9> textures;
Later, I am passing a LPDIRECT3DTEXTURE9
object to it, like so textures.push_back(texture);
后来,我传递了一个
LPDIRECT3DTEXTURE9
对象,就像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. 它在
size()
函数的vector类中打破。 Why might this happen? 为什么会这样?
Edit: 编辑:
I also run into an identical problem performing the same operation on a vector of D3DXVECTOR3
objects. 我也遇到了一个相同的问题,对
D3DXVECTOR3
对象的矢量执行相同的操作。 Since LPDIRECT3DTEXTURE9
is a pointer to an IDIRECT3DTEXTURE9
, should I be using that instead? 由于
LPDIRECT3DTEXTURE9
是指向IDIRECT3DTEXTURE9
的指针,我应该使用它吗?
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(). 好吧,因为它的匈牙利名字的LPDIRECT3DTEXTURE9是一个指针而不是一个对象(正如你所指的那样),我的猜测是你在调用push_back()之前传递已经在你的可怜的矢量对象上做了一个fandango的无效指针。
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.
并且,是的,push_back()应该只能在内存不足或尝试在向量中使用不可复制或不可分配的对象时失败,然后才能通过访问冲突。
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).
我建议在其内部放置一个数据观察点,以查看它上面的内容(在调试器中)。
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. 在这种情况下,
textures
似乎是SpriteManager
类的成员。 So, that suggest you actually don't have a SpriteManager
object either. 所以,这表明你实际上也没有
SpriteManager
对象。 Is the this
pointer valid? this
指针有效吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.