0

So I tried "putting together" a chess game with a simple AI (Minimax algo) and it gave me the above error. I am kinda clueless as to how to proceed. Also please ignore the dummy print statements. TIA!


    std::vector<move> possibleMoves(char pieceColor)
    {   std::vector<move> output;
        move toAdd;
        for (int iRow = 0; iRow < 8; ++iRow) {
            for (int iCol = 0; iCol < 8; ++iCol) {
                if (MainGameBoard[iRow][iCol] != 0) {
                    // If it is a piece of the current player, see if it has a legal move
                    if (MainGameBoard[iRow][iCol]->GetColor() == pieceColor) {
                        for (int iMoveRow = 0; iMoveRow < 8; ++iMoveRow) {
                            for (int iMoveCol = 0; iMoveCol < 8; ++iMoveCol) {
                                if (MainGameBoard[iRow][iCol]->IsLegalMove(iRow, iCol, iMoveRow, iMoveCol, MainGameBoard)) {
                                    // Make move and check whether king is in check
                                    GamePiece* qpTemp                   = MainGameBoard[iMoveRow][iMoveCol];
                                    MainGameBoard[iMoveRow][iMoveCol]   = MainGameBoard[iRow][iCol];
                                    MainGameBoard[iRow][iCol]           = 0;
                                    bool bCanMove = !IsInCheck(pieceColor);
                                    // Undo the move
                                    MainGameBoard[iRow][iCol]           = MainGameBoard[iMoveRow][iMoveCol];
                                    MainGameBoard[iMoveRow][iMoveCol]   = qpTemp;
                                    if (bCanMove) {
                                       toAdd.srcrow=iRow;
                                       toAdd.srccol=iCol;
                                       toAdd.destrow=iMoveRow;
                                       toAdd.destcol=iMoveCol;
                                       output.push_back(toAdd);

                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    return output;
    }

     move minimax(int depth, bool minimize){
        move best_move;
        best_move.score = -1000000 + 2000000*minimize;
        if(0 == depth){
            best_move.score = score();
            return best_move;
        }




            for(auto & to : possibleMoves('B')){

This is the line that might be giving the error:

                CBoard branch = *this;
                branch.makemove(to);
                move option = branch.minimax(depth-1, !minimize);
                if((option.score > best_move.score && !minimize) || (option.score < best_move.score && minimize)){
                    best_move.score = option.score;
                    best_move.srccol = to.srccol;
                    best_move.destcol = to.destcol;
                    best_move.srcrow = to.srcrow;
                    best_move.destrow = to.destrow;
                    
                }
            }
        
        return best_move;
    }

  
    }

Link to whole code:

https://pastebin.com/M2EN1Jnk

Shyam
  • 1
  • 2
  • Open up `gdb` or some other debugger and run it. That error will come with a lovely stack trace in `gdb` where you can see exactly what line it's happening on and where it was called from. No guesswork required. – Silvio Mayolo Apr 12 '22 at 02:31
  • @SilvioMayolo I have edited the code and specified the error. Thanks for suggesting the debugger. – Shyam Apr 12 '22 at 03:19
  • Looks like `CBoard::~Cboard()` will delete any `GamePiece` objects referenced by its `MainGameBoard`. But you make multiple boards based on the same pieces. `branch = *this;` is called but you have no non-trivial copy constructor, so you will get a default copy which just copies the pointers in `MainGameBoard`. Every `CBoard` refers to the same `GamePiece` objects, just from different squares. The first one that falls out of scope and gets destructed will destroy (delete) all the `GamePiece` objects. Your error comes from calling a method on a deleted object. – Wyck Apr 12 '22 at 03:30
  • One way to solve this particular problem is to switch from raw pointers to `std::shared_ptr` and to stop using `delete`. – n. m. could be an AI Apr 12 '22 at 04:45
  • @Wyck thanks for the suggestion! I was able to get it working within minutes <3 – Shyam Apr 19 '22 at 16:52

0 Answers0