在C++中对STL列表使用push_back()会导致访问冲突,崩溃

1 c++ stl list push-back

我正在使用我自己的自制游戏引擎创建游戏,但是我在使用列表时遇到了麻烦.

我的程序中有一个名为BoardState的结构.这些结构中的每一个都有一个名为children的BoardState指针列表.这是因为我为我的游戏的AI创建了一个BoardStates树.

为了帮助创建我的树,我有一个名为MakeBoard的函数.该函数传递了创建新板所需的所有信息,然后它应该将指向新板的指针添加到父板子列表的末尾.这是相关的功能,MakeBoard:

void MakeBoard(BoardState* pStartBoard, int iPiece, int iPosStart, int iPosFinish, int* pJumpArray)
Run Code Online (Sandbox Code Playgroud)

{

//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);
Run Code Online (Sandbox Code Playgroud)

}

额外评论的部分是我尝试其他想法,看看它们是否有效.

不幸的是,这会导致程序抛出以下错误:

访问冲突读取位置0xcdcdcdd1.

如果我深入研究调试器,我会发现问题出现在STL列表文件中.这些是调用堆栈中的前三个调用:

OpenGL_Engine_Test1.exe!std :: list> :: _ Insert(std :: list> :: _ Const_iterator <1> _Where = ...,tagBoardState*const&_Val = 0x049a1a80)718行+ 0x10字节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++
Run Code Online (Sandbox Code Playgroud)

然后打开定义列表的文件,并指出_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;
    }
Run Code Online (Sandbox Code Playgroud)

除此之外,我真的不知道更多.我不知道为什么会出现这个问题.我知道"访问冲突"基本上意味着我要么尝试访问不存在的东西,我无法访问,或者存在某种范围问题,但我看不出有什么这些是适用的.

如果有人能指出我正确的方向,我会非常感激.我已经做了很多搜索,但我发现的几乎所有内容都与向量有关,而且似乎不是我的问题.

erg*_*sys 6

如果malloc()是一个C++类,则不会为该类的任何字段调用构造函数,包括问题向量.你需要使用新的.

我假设pStartBoard的分配与pNewBoard相同,但即使不是这样,你也会在pNewBoard中遇到同样的问题.