本文介绍了在C ++中将push_back()用于STL列表会导致访问冲突,崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 我正在使用自己的自制游戏引擎来创建游戏,但是在使用列表时遇到了麻烦.I'm creating a game using my own homemade gaming engine, but I'm running into trouble using lists.我的程序中有一个名为BoardState的结构.这些结构中的每一个都有一个称为子项的BoardState指针列表.这是因为我为游戏的AI创建了BoardStates树.I have a structure in my program called BoardState. Each of these structures has a list of BoardState pointers called children. This is because I create a tree of BoardStates for the AI of my game.为帮助创建树,我有一个名为MakeBoard的函数.此函数将传递创建新板所需的所有信息,然后将指向该新板的指针添加到父板子级列表的末尾.这是相关的功能MakeBoard:To aid in the creation of my tree, I have a function called MakeBoard. This function gets passed all the information it needs to create a new board, and then it should add the pointer to that new board to the end of the parent board's children list. Here is the relevant function, 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);}额外注释的部分是我尝试其他想法以查看它们是否起作用的地方.The extra commented parts are where I was trying out other ideas to see if they worked.不幸的是,这导致程序抛出以下错误:Unfortunately this causes the program to throw the following error:如果我深入调试器,则会发现问题出在STL列表文件中.这些是调用堆栈中的前三个调用:If I dig into the debugger, I find out that the problem is occurring in the STL list file. These are the top three calls in the call stack: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++然后打开定义列表的文件,并指出_insert函数内部的问题行:It then opens up the file where list is defined, and points out the problem line inside the _insert function: #if _HAS_ITERATOR_DEBUGGING 如果(_Where._Mycont!= this) _DEBUG_ERROR(列表插入迭代器超出范围"); #endif/* _HAS_ITERATOR_DEBUGGING */#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; }除此之外,我真的不了解更多.我不知道为什么会出现此问题.我知道访问冲突"基本上意味着我正在尝试访问不存在的东西,我无权访问或存在某种范围问题,但我看不到如何解决其中的适用.Beyond this, i don't really know more. I have no idea why this problem is occurring. I know that an "Access Violation" basically means that I'm either trying to access something that doesn't exist, I don't have access to, or there is some sort of scope problem, but I can't see how any of those are applicable.如果有人能指出我正确的方向,我将不胜感激.我已经做了很多搜索,但是我发现的几乎所有内容都与Vectors有关,而这似乎并不是我的问题.If anyone can point me in the right direction, I would really appreciate it. I've done a lot of searching, but almost everything I've found has been pertaining to Vectors and haven't seemed to be my issue exactly.推荐答案如果您malloc()一个C ++类,则该类的任何字段(包括问题向量)都不会调用构造函数.您需要使用新的.If you malloc() a C++ class, no constructors will be called for any of that class's fields, including your problem vector. You need to use new.我已经假设pStartBoard的分配与pNewBoard相同,但是即使不是这种情况,您在pNewBoard中也会遇到相同的问题.I've made an assumption that pStartBoard was allocated the same as pNewBoard, but you will have the same problem in pNewBoard even if this is not the case. 这篇关于在C ++中将push_back()用于STL列表会导致访问冲突,崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-06 11:47