简体   繁体   English

std::vector 断言失败(向量迭代器不兼容)

[英]std::vector Assertion failed (vector iterators incompatible)

I have this struct:我有这个结构:

struct MxMInstanceData
{
    D3DXVECTOR2 mTransform;
    float mSpacing;
};

Then I create a vector of MxMInstanceData:然后我创建一个 MxMInstanceData 向量:

std::vector<MxMInstanceData> instInFrustumData;

If I call instInFrustumData.clear() I get this error:如果我调用instInFrustumData.clear()我得到这个错误:

Assertion failed (vector iterators incompatible)断言失败(向量迭代器不兼容)

Vector creation code:矢量创建代码:

instInFrustumData.reserve(mNumInstances);

Vector update code:矢量更新代码:

void Terrain::updateInstances()
{
    mNumInstancesInFrustum = 0;

    if(instInFrustumData.size() != 0)
        instInFrustumData.clear();

    mpMxMInstInFrustumB->Map(D3D10_MAP_WRITE_DISCARD, NULL, (void**) &instInFrustumData);

    for(int x = 0; x < mNumInstances; x++)
    {
        if(mpCamera->point2DInFrustum(instData[x].mTransform + 
            D3DXVECTOR2(instData[x].mSpacing/2 + mpCamera->getPosition().x, instData[x].mSpacing/2 + mpCamera->getPosition().z), instData[x].mSpacing/2)
            != OUTSIDE)
        {
            instInFrustumData.push_back(instData[x]);
            mNumInstancesInFrustum++;
        }
    }

    mpMxMInstInFrustumB->Unmap();
}

What can make this happen?什么能让这发生?

And in the destructor of my class I also call clear()在我的 class 的析构函数中,我还调用 clear()

Kind of guessing here, but maybe your problem is this line:有点猜测,但也许你的问题是这一行:

mpMxMInstInFrustumB->Map(D3D10_MAP_WRITE_DISCARD, NULL, (void**) &instInFrustumData);

You're passing a pointer to the vector itself to this Map function, which I'm guessing might be overwriting some of its internals?你将一个指向向量本身的指针传递给这个 Map function,我猜这可能会覆盖它的一些内部结构? I don't have its documentation, but it doesn't look like a function that's expecting a pointer to a vector:)我没有它的文档,但它看起来不像 function 期望指向向量的指针:)

You may want to check out a reference on using std::vector like http://www.cplusplus.com/reference/stl/vector/ or buy a good STL book.您可能想查看有关使用std::vector的参考,例如http://www.cplusplus.com/reference/stl/vector/或购买一本好的 STL 书。 You are using some methods in what I would consider unorthodox ways.您正在以我认为非正统的方式使用一些方法。

  • Use empty() to check if a vector has elements (if not empty clear just reads better)使用empty()检查向量是否有元素(如果不为空,则清除更好)
  • Use locally scoped variables when possible (things that don't need to stay in scope shouldn't)尽可能使用局部范围的变量(不需要保留在 scope 中的东西不应该)
  • Use STL iterators or container sizes in loops (is having two incrementing integers in one loop needed?)在循环中使用 STL 迭代器或容器大小(是否需要在一个循环中使用两个递增整数?)
  • Use the "best" STL container for your implementation (do you want vectors or maps here?)使用“最佳” STL 容器进行实施(您想要向量还是地图?)
  • Avoid C-style casts and misuse of objects ( (void**) &instInFrustumData is a very bad idea)避免 C 风格的强制转换和滥用对象( (void**) &instInFrustumData是一个非常糟糕的主意)

You have so many members variables whose definition is unknown as well unknown methods Map() and UnMap() and still haven't shown any code using iterators related to your original error.您有这么多成员变量,其定义未知以及未知方法Map()UnMap()并且仍然没有显示任何使用与原始错误相关的迭代器的代码。 I would guess your use of instData[x] is dangerous and problematic as well as the way that loop is constructed in general.我猜你对instData[x]的使用是危险的和有问题的,以及循环的一般构造方式。 You also really don't want to be treating STL containers as anything but STL containers.您也真的不想将 STL 容器视为 STL 容器。 Things like (void**) &instInFrustumData should be avoided as they can only cause problems.应该避免像(void**) &instInFrustumData这样的事情,因为它们只会导致问题。

I highly suggest you learn C++ first before tackling DirectX or graphics and game engines written in both.我强烈建议您先学习 C++,然后再处理 DirectX 或两者编写的图形和游戏引擎。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM