开发者

Using push_back() for STL List in C++ causes Access Violation, Crash

开发者 https://www.devze.com 2023-04-11 19:54 出处:网络
I\'m creating a game using my own homemade gaming engine, but I\'m running into trouble using lists. I have a structure in my program called BoardState. Each of these structures has a list of BoardSt

I'm creating a game using my own homemade gaming engine, but I'm running into trouble using lists.

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.

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->ch开发者_运维技巧ildren.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:

Access violation reading location 0xcdcdcdd1.

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 >::_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++

It then opens up the file where list is defined, and points out the problem line inside the _insert function:

void _Insert(const_iterator _Where, const _Ty& _Val) { // insert _Val at _Where

#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.

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.


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.

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.


Access violation reading location 0xcdcdcdd1. Indicates an unitialized variable. The debugger puts in values like that as markers. Unfortunately, I can't deduce where you are seeing the error, nor any STL containers in your code sample. But 'pNewBoard->posArray' would be the first place I look. Does that class's constructor set that member to something?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号