调试断言失败,可能指针问题?

时间:2015-03-03 06:43:38

标签: c++ debugging pointers sfml assertion

这是我认为是导致问题的一个类的函数:

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();

    }   
}
}

我认为问题出在某处。我找不到确切的问题,甚至在什么情况下也不会发现问题,因为它似乎与发生的时间不一致。

这也出现在错误中:

表达式:_BLOCK_TYPE_IS_VALID(pHead-&gt; nBlockUse)

如果重要的话,我会使用SFML图形库。

1 个答案:

答案 0 :(得分:0)

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

此行tetriminoInPlay指向内存中您不再使用的空格。然而,在下一次迭代中,您使用此变量。你需要解决这个问题。

您还需要确定时间。计数器是一种糟糕的做事方式,因为你的游戏在慢速机器上会更容易,在高端游戏机上也无法播放。看看here如何在游戏中实现时间。