开发者

std::vector Assertion failed (vector iterators incompatible)

I have this struct:

struct MxMInstanceData
{
    D3DXVECTOR2 mTransform;
    float mSpacing;
};

Then I create a vector of MxMInstanceData:

std::vector<MxMInstanceData> instInFrustumData;

If I call instInFrustumData.clear() I get this error:

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].mSpacin开发者_开发技巧g/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()


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. 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)
  • Use locally scoped variables when possible (things that don't need to stay in scope shouldn't)
  • Use STL iterators or container sizes in loops (is having two incrementing integers in one loop needed?)
  • Use the "best" STL container for your implementation (do you want vectors or maps here?)
  • Avoid C-style casts and misuse of objects ((void**) &instInFrustumData is a very bad idea)

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. I would guess your use of instData[x] is dangerous and problematic as well as the way that loop is constructed in general. You also really don't want to be treating STL containers as anything but STL containers. Things like (void**) &instInFrustumData should be avoided as they can only cause problems.

I highly suggest you learn C++ first before tackling DirectX or graphics and game engines written in both.


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? I don't have its documentation, but it doesn't look like a function that's expecting a pointer to a vector :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜