Segmentation Fault Drawing Objects with GDI+

-1

I am working on coding a checkers game.

I have the board drawing on the screen along with the pieces in their starting positions. I want to redraw the board every time a piece is moved.

When I run my function to redraw the board I get the following error. It is occurring when I use the GDIplus function .get() on a pointer to a bitmap:

Exception thrown at 0x6815CD2F (GdiPlus.dll) in Step2.exe: 0xC0000005: Access violation reading location 0xCCCCCCD0.

I push the tiles and game pieces back into a vector like so:

void CBoard::getPieces()
{
    while(mGamePieces.size() < 12)
    {

        CWhitePiece* white = new CWhitePiece();
        mGamePieces.push_back(white);
    }
    while (mGamePieces.size() < 24)
    {

        CBrownPiece* brown = new CBrownPiece();
        mGamePieces.push_back(brown);
    }
}

To draw the game pieces, Draw is called on each game piece:

void CGamePiece::Draw(Gdiplus::Graphics * graphics)
{
    double wid = 100;
    double hit = 100;
    graphics->DrawImage(mImage.get(),
        float(mTile->getColumn() / 2) + 100 * mTile->getColumn(), float(mTile->getRow() / 2) + 100 * mTile->getRow(),
        100.0, 100.0);
}

It draws perfectly the first time. To move a piece, I change the tile it is associated with (which will change the location of where it's drawn) and then try to redraw the board. When being redrawn, the segmentation fault occurs when .get() is called on mImage, which is of type std::unique_ptr, and set when the game piece is created.

I realized if I try to draw the board multiple times BEFORE trying to move a piece, I do not get this error. It only occurs after I try to move a piece.

right now here is all I have in move():

void CBoard::move()
{
    // Find piece to move
    // Display possible destinations
    shared_ptr<CGamePiece> gamePiece = findPieceToMove();


    if (gamePiece != nullptr && gamePiece->getColor() == "Brown")
    {

        DrawPieces(mGraphics);
    }
    else if (gamePiece != nullptr && gamePiece->getColor() == "White")
    {

        DrawPieces(mGraphics);
    }


}
c++
gdi+
asked on Stack Overflow May 9, 2019 by Mari • edited May 9, 2019 by Mari

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0