Valgrind가 잘못된 읽기 오류를 감지했습니다. 수정 방법이나 정확한 방법을 모릅니다. 문제가 무엇인지 알 수 없습니다.Valgrind가 간단한 반복기 클래스에서 유효하지 않은 읽기 오류를 감지합니다.
Invalid read of size 8
at 0x443212: std::vector<Tile*, std::allocator<Tile*> >::end() const
by 0x44296C: Collection<Tile*>::Iterator::operator++()
Iterator 클래스는 매우 간단하지만 (실제로는 다소 나쁜 프로그래밍 요소이지만) 지금 당장 필요합니다.
while (mY != mX->end() && *mY == 0) ++mY;
... 반복자의 다음 Valgrind의 오류를 일으키는
Iterator(size_t x, size_t y, const TileCollection& tiles)
: mTiles(&tiles)
, mX(mTiles->begin())
, mY(mTiles->at(x).begin())
{
std::advance(mX, x);
std::advance(mY, y);
bool foundFirst = false;
while (!foundFirst)
{
while (mY != mX->end() && *mY == 0) ++mY;
if (mY != mX->end()) foundFirst = true;
else
{
++mX;
if (mX != mTiles->end()) mY = mX->begin();
}
}
}
Iterator Iterator::operator++()
{
bool foundNext = false;
++mY;
while (!foundNext)
{
while (mY != mX->end() && *mY == 0) ++mY;
if (mY != mX->end()) foundNext = true;
else
{
++mX;
if (mX != mTiles->end()) mY = mX->begin();
}
}
return *this;
}
void TileCollection::add(Tile* tile)
{
Point2D p(tile->getPosition());
std::vector<Tile*> tmp(1, (Tile*)0);
if ((size_t)p.x >= mTiles.size())
mTiles.resize(p.x + 1, tmp);
if ((size_t)p.y >= mTiles.at(p.x).size())
mTiles.at(p.x).resize(p.y + 1, (Tile*)0);
mTiles.at(p.x).at(p.y) = tile;
++mNumTiles;
}
실제 코드 라인이있다 : 나는 희망이 내 문제를 찾을 수 있도록 당신이 알아야 할 세 가지 방법이 있다고 생각합니다 : : 연산자 ++ 메서드.
감사합니다. 귀하의 대답은 정확합니다. 적절한 else 절을 추가하면 문제가 해결됩니다. – BrightBit