我正在使用自己的自制游戏引擎创建游戏,但在使用列表时遇到了麻烦。
我的程序中有一个名为 BoardState 的结构。这些结构中的每一个都有一个称为子项的 BoardState 指针列表。这是因为我为我的游戏 AI 创建了一个 BoardStates 树。
为了帮助创建我的树,我有一个名为 MakeBoard 的函数。这个函数获得了创建新板所需的所有信息,然后它应该将指向该新板的指针添加到父板的子列表的末尾。这是相关的功能,MakeBoard:
void MakeBoard(BoardState* pStartBoard, int iPiece, int iPosStart, int iPosFinish, int* pJumpArray)
{
//BoardState* pNewBoard = &NewBoard;
//pNewBoard->bPlayerTurn = !(pStartBoard->bPlayerTurn);
//NewBoard.bPlayerTurn = !(pStartBoard->bPlayerTurn);
BoardState* pNewBoard = (BoardState*)malloc(sizeof(BoardState));
pNewBoard->bPlayerTurn = !(pStartBoard->bPlayerTurn);
// Copy the BoardPositions of the starting board into the new Board.
for(int i = 0; i < 37; i++)
{
pNewBoard->posArray[i] = pStartBoard->posArray[i];
//NewBoard.posArray[i] = pStartBoard->posArray[i];
}
// Make the BoardPosition change necessary to reflect the move.
pNewBoard->posArray[iPosStart] = -1;
pNewBoard->posArray[iPosFinish] = iPiece;
//NewBoard.posArray[iPosStart] = -1;
//NewBoard.posArray[iPosFinish] = iPiece;
// Now account for any pieces that were jumped, if applicable.
if(pJumpArray != NULL)
{
for(int i = 0; i < 16; i++)
{
if(pJumpArray[i] != -1)
{
pNewBoard->posArray[pJumpArray[i]] = -1;
//NewBoard.posArray[pJumpArray[i]] = -1;
}
}
}
// Connect the parent board to this child board.
pNewBoard->parent = pStartBoard;
//NewBoard.parent = pStartBoard;
//pStartBoard->children.push_back(_pTestState);
pStartBoard->children.push_back(pNewBoard); // <- The problem
//pStartBoard->children.push_back(&NewBoard);
}
额外注释的部分是我尝试其他想法以查看它们是否有效的地方。
不幸的是,这会导致程序抛出以下错误:
访问冲突读取位置 0xcdcdcdd1。
如果我深入调试器,我会发现问题出在 STL 列表文件中。这些是调用堆栈中的前三个调用:
OpenGL_Engine_Test1.exe!std::list >::_Insert(std::list >::_Const_iterator<1> _Where=..., tagBoardState * const & _Val=0x049a1a80) Line 718 + 0x10 bytes C++
OpenGL_Engine_Test1.exe!std::list<tagBoardState *,std::allocator<tagBoardState *> >::push_back(tagBoardState * const & _Val=0x049a1a80) Line 670 + 0x51 bytes C++
OpenGL_Engine_Test1.exe!MakeBoard(tagBoardState * pStartBoard=0x049a0580, int iPiece=16, int iPosStart=21, int iPosFinish=16, int * pJumpArray=0x00000000) Line 352 C++
然后它打开定义 list 的文件,并指出 _insert 函数内的问题行:
void _Insert(const_iterator _Where, const _Ty& _Val) { // 在 _Where 处插入 _Val
#if _HAS_ITERATOR_DEBUGGING if (_Where._Mycont != this) _DEBUG_ERROR("list insert iterator outside range"); #endif /* _HAS_ITERATOR_DEBUGGING */
_Nodeptr _Pnode = _Where._Mynode();
_Nodeptr _Newnode = _Buynode(_Pnode, _Prevnode(_Pnode), _Val); // PROBLEM
_Incsize(1);
_Prevnode(_Pnode) = _Newnode;
_Nextnode(_Prevnode(_Newnode)) = _Newnode;
}
除此之外,我真的不知道更多。我不知道为什么会出现这个问题。我知道“访问冲突”基本上意味着我要么试图访问不存在的东西,我无权访问,要么存在某种范围问题,但我看不出有什么其中适用。
如果有人能指出我正确的方向,我将不胜感激。我做了很多搜索,但我发现的几乎所有东西都与向量有关,而且似乎并不是我的问题。