在c++中对STL List使用push_back()会导致访问冲突,崩溃
Using push_back() for STL List in C++ causes Access Violation, Crash
我正在使用自己自制的游戏引擎创造一款游戏,但是我在使用列表时遇到了麻烦。
我的程序中有一个结构体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::列表>::_Insert (std::列表>::_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函数中的问题行:
如果_HAS_ITERATOR_DEBUGGINGvoid _Insert(const_iterator _Where)const _Ty&_Val){//insert _Val at _Where
#如果(_Where。= this)_DEBUG_ERROR("list insert iterator out of 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;
}
除此之外,我真的不知道更多。我不知道为什么会出现这个问题。我知道"访问冲突"基本上意味着我要么试图访问不存在的东西,我没有访问权限,要么存在某种范围问题,但我看不出这些是如何适用的。
如果有人能给我指出正确的方向,我将非常感激。我已经做了很多搜索,但几乎所有我发现的已经有关向量,并没有似乎是我的问题。如果malloc()一个c++类,则不会为该类的任何字段调用构造函数,包括问题向量。你需要用新的。
我已经做了一个假设,pStartBoard被分配相同的pNewBoard,但你会有相同的问题在pNewBoard即使这不是这种情况。
访问违规读取位置0xcdcdcdd1。指示未初始化的变量。调试器将这样的值作为标记。不幸的是,我无法推断出您在哪里看到错误,也无法推断出代码示例中的任何STL容器。但'pNewBoard->posArray'会是我第一个看的地方。类的构造函数是否将该成员设置为什么?
- 写入位置0x0000000C时发生访问冲突
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++ 中动态二维数组的访问冲突
- 从嵌套循环中的 std::list 中删除将返回访问冲突
- 使用向量的函数崩溃,调试器说访问冲突读取位置
- Windows C++程序中的访问冲突是否总是立即崩溃
- GetMonitorBrightness() 因访问冲突而崩溃
- 中止回复崩溃并出现访问冲突
- MEMCPY 后崩溃:访问冲突读取位置
- 创建对象时发生SSE访问冲突崩溃
- C++访问冲突崩溃
- Writefile导致崩溃,并导致访问冲突
- 在c++中对STL List使用push_back()会导致访问冲突,崩溃
- 访问计算机指针时发生访问冲突(c系统异常代码:c0000005),导致应用程序崩溃
- c++, WIN32, WndProc到成员函数崩溃(访问冲突0x88)
- 与JNI崩溃服务器JVM的访问冲突
- 通过WinAPI套接字连接到蓝牙设备时崩溃(读取位置0x00000004时发生访问冲突)