0

This is a function from a class that I believe to be the cause of the problem:

void Game::processGameScreen()
{
    cout << "\nGAME PROCESS STARTED";
    tetriminoInPlay = new Tetrimino;
    tetriminoOnDeck = new Tetrimino;
    int count = 0;
while (window.isOpen() && gameWell.topReached() == false)
{
    //check for events and create tetrimino pointers
    sf::Event event;

    while (window.pollEvent(event)) {
        //check for arrow key presses
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
        {
            tetriminoInPlay->rotateRight();
            if (gameWell.tetriminoFit(*tetriminoInPlay) == false)
                tetriminoInPlay->rotateLeft();
        }
        else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Left)
        {
            tetriminoInPlay->moveLeft();
            if (gameWell.tetriminoFit(*tetriminoInPlay) == false)
                tetriminoInPlay->moveRight();
        }
        else if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Right)
        {
            tetriminoInPlay->moveRight();
            if (gameWell.tetriminoFit(*tetriminoInPlay) == false)
                tetriminoInPlay->moveLeft();
        }
        else if (event.type == sf::Event::Closed)
            window.close();

        //counter to move pieces down
        cout << "\n" << count;
        if (count % 15 == 0 ) {
            cout << "\nMOVING DOWN";
            tetriminoInPlay->moveDown();
            //check if it fits
            if (gameWell.tetriminoFit(*tetriminoInPlay) == false) {
                tetriminoInPlay->moveUp();
                gameWell.addTetriminoToWell(*tetriminoOnDeck);
                delete tetriminoInPlay;
                score = score + gameWell.clearFullRows(); //add combo scoring later
                //check for game over
                if (gameWell.topReached() == false) {
                    tetriminoInPlay = tetriminoOnDeck;
                    tetriminoInPlay->setLocation(0,4);
                    delete tetriminoOnDeck; 
                    tetriminoOnDeck = new Tetrimino;
                }
            }
        }
        count++;
        //clear window, draw well, tetro, window.display
        window.clear(sf::Color::White);
        //draw tetriminoOnDeck !!!Figure out where to draw this at some point!!!
        //drawTetrimino(tetriminoOnDeck, LAYOUT_BOARD_TOP, LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS);
        drawWell(gameWell, LAYOUT_BOARD_TOP, LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS);
        drawTetrimino(tetriminoInPlay, LAYOUT_BOARD_TOP, LAYOUT_BOARD_LEFT, BLOCK_SIZE_PIXELS);
        drawScore(score, 50, 50);
        window.display();

    }   
}
}

I believe the issue is with pointers somewhere. I can't locate the exact problem or even under what circumstance the problem occurs, as it is seemingly inconsistent on when it happens.

This is also present in the error:

Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

I'm using the SFML graphics library if that matters.

Zearia
  • 123
  • 7
  • That assert generally means you are corrupting memory somewhere. Usually writing outside the bounds of an array. If you run it in a debugger you will be able to see what line on your code is triggering the issue. That doesn't mean that's where you corrupted memory, but it can be helpful to give you a place to start looking. – Retired Ninja Mar 03 '15 at 06:51

1 Answers1

0
tetriminoInPlay = tetriminoOnDeck;
tetriminoInPlay->setLocation(0,4);
delete tetriminoOnDeck; 
tetriminoOnDeck = new Tetrimino;

After this lines tetriminoInPlay points to a space in memory where you no longer should work with. Yet, the next iteration, you use this variable. You will need to fix this.

You also need to fix your timing. The counter is a bad way of doing stuff because your game will be easier on slow machines and unplayable on highend gaming computers. Have a look here on how to implement time in a game.

Community
  • 1
  • 1
nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Sorry if I have trouble understanding these things, I'm really new to programming. Why does this not work though? If I set the tetriminoInPlay = tetriminoOnDeck, then delete tetriminoOnDeck and set it to a new Tetrimino, I don't see how there's memory I'm no longer working with? I still want to use the memory of tetriminoOnDeck as tetriminoInPlay for the next loop so I set it equal to tetriminoInPlay and then delete it and set it again. Also, in the constructor of the class I limit the frame rate to 30 which I think limits the times it can loop through to 30 times per second. – Zearia Mar 03 '15 at 06:59
  • `tetriminoInPlay = tetriminoOnDeck` means both pointers point to *the same instance in memory* . Calling `delete` on one of them will delete the instance both are pointing to. That means both are pointing to the same invalid location now. – nvoigt Mar 03 '15 at 07:10