SFML Access violation

623 views Asked by At

I'm making a tetris game in C++ and in the DrawBoard() function I'm getting an error that reads: Exception thrown at 0x0F52DA36 (sfml-graphics-d-2.dll) in Tetris_C++.exe: 0xC0000005: Access violation writing location 0x013EDA88.

If there is a handler for this exception, the program may be safely continued.

Here's the code:

void DrawBoard() {

for (int i = 0; i < boardWidth; i++)
{
    for (int j = 0; j < boardHeight; i++)
    {
        switch (board[i][j]) {//What's wrong with this?
        case 'b':
            squares[i][j].setFillColor(sf::Color::Blue);
            break;
        case 'c':
            squares[i][j].setFillColor(sf::Color::Cyan);
            break;
        case 'y':
            squares[i][j].setFillColor(sf::Color::Yellow);
            break;
        case 'o':
            squares[i][j].setFillColor(sf::Color(255, 165, 0));//Orange
            break;
        case 'p':
            squares[i][j].setFillColor(sf::Color(150, 50, 250));//Purple
            break;
        default:
            break;
        }
    }
}

for (int x = 0; x < boardWidth; x++) {
    for (int y = 0; y < boardHeight; y++) {
        window.draw(squares[x][y]);
    }
}

}

2

There are 2 answers

0
Jesper Juhl On

The error simply means that your program tried to access memory outside its allocated address space. But without seeing a SSCCE it's impossible to know where your error is. A guess would be that one of your variables have gone out of scope or that you are trying to access outside the bounds of your squares array - but again, that's only a guess based on incomplete info.

1
Lorence Hernandez On
for (int j = 0; j < boardHeight; i++)

this is what makes you get a segmentation fault (im sure its a typo)

you only get 1 iterate for your outer loop, and then the inner loop makes the segfault by incrementing i until it is become equal to boardwidth

the code is self explanatory, so im sure this should be:

for (int j = 0; j < boardHeight; j++)