title: Using push_back() for STL List in C++ causes Access Violation, Crash
body: <p>I'm creating a game using my own homemade gaming engine, but I'm running into trouble using lists.</p>
  
  <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>void MakeBoard(BoardState* pStartBoard, int iPiece, int iPosStart, int iPosFinish, int* pJumpArray)
  </code></pre>
  
  <p>{</p>
  
  <pre><code>//BoardState* pNewBoard = &amp;NewBoard;
  //pNewBoard-&gt;bPlayerTurn = !(pStartBoard-&gt;bPlayerTurn);
  //NewBoard.bPlayerTurn = !(pStartBoard-&gt;bPlayerTurn);
  BoardState* pNewBoard = (BoardState*)malloc(sizeof(BoardState));
  
  pNewBoard-&gt;bPlayerTurn = !(pStartBoard-&gt;bPlayerTurn);
  
  // Copy the BoardPositions of the starting board into the new Board.
  for(int i = 0; i &lt; 37; i++)
  {
      pNewBoard-&gt;posArray[i] = pStartBoard-&gt;posArray[i];
      //NewBoard.posArray[i] = pStartBoard-&gt;posArray[i];
  }
  
  // Make the BoardPosition change necessary to reflect the move.
  pNewBoard-&gt;posArray[iPosStart] = -1;
  pNewBoard-&gt;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 &lt; 16; i++)
      {
          if(pJumpArray[i] != -1)
          {
              pNewBoard-&gt;posArray[pJumpArray[i]] = -1;
              //NewBoard.posArray[pJumpArray[i]] = -1;
          }
      }
  }
  
  // Connect the parent board to this child board.
  pNewBoard-&gt;parent = pStartBoard;
  //NewBoard.parent = pStartBoard;
  
  //pStartBoard-&gt;children.push_back(_pTestState);
  
  pStartBoard-&gt;children.push_back(pNewBoard); // &lt;- The problem
  
  //pStartBoard-&gt;children.push_back(&amp;NewBoard);
  </code></pre>
  
  <p>}</p>
  
  <p>The extra commented parts are where I was trying out other ideas to see if they worked.</p>
  
  <p>Unfortunately this causes the program to throw the following error:</p>
  
  <blockquote>
    <p>Access violation reading location 0xcdcdcdd1.</p>
  </blockquote>
  
  <p>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:</p>
  
  <blockquote>
    <blockquote>
      <p>OpenGL_Engine_Test1.exe!std::list >::_Insert(std::list >::_Const_iterator&lt;1> _Where=..., tagBoardState * const &amp; _Val=0x049a1a80)  Line 718 + 0x10 bytes    C++</p>
    </blockquote>
  </blockquote>
  
  <pre><code>OpenGL_Engine_Test1.exe!std::list&lt;tagBoardState *,std::allocator&lt;tagBoardState *&gt; &gt;::push_back(tagBoardState * const &amp; _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++
  </code></pre>
  
  <p>It then opens up the file where list is defined, and points out the problem line inside the _insert function:</p>
  
  <blockquote>
    <p>void _Insert(const_iterator _Where,
            const _Ty&amp; _Val)
            {   // insert _Val at _Where</p>
  </blockquote>
  
  <p>#if _HAS_ITERATOR_DEBUGGING
          if (_Where._Mycont != this)
              _DEBUG_ERROR("list insert iterator outside range");
   #endif /* _HAS_ITERATOR_DEBUGGING */</p>
  
  <pre><code>    _Nodeptr _Pnode = _Where._Mynode();
      _Nodeptr _Newnode = _Buynode(_Pnode, _Prevnode(_Pnode), _Val); // PROBLEM
      _Incsize(1);
      _Prevnode(_Pnode) = _Newnode;
      _Nextnode(_Prevnode(_Newnode)) = _Newnode;
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>