C++:损坏的双链表和内存损坏

C++: corrupted double-linked list and memory corruption

本文关键字:损坏 内存 链表 C++      更新时间:2023-10-16

我知道这个问题在stackoverflow中有答案,但我仍然无法解决我的问题,我没有看到错误。我正在使用vector,我认为我使用它是正确的,我没有做任何免费或malloc/calloc之类的事情。然而,我确信问题出在向量上,做了一些错误的事情。最奇怪的部分是对程序进行调试,它出现在两三个不同的站点。

我在linux上使用SDL2和默认的gcc编译代码。我正在使用Netbeans,使用命令

g++ -std=c++11 -g -Wall   -c -g -MMD -MP -MF "path/src/game/Game.o.d" -o path/src/game/Game.o src/game/Game.cpp

并与-lSDL2-lSDL2_image 链接

错误,其中一个

malloc(): memory corruption (fast): 0x0000000000d86d50 ***
corrupted double-linked list: 0x00000000013fb5d0 ***

或不太常见:[link][1]

Valgrind展示了这样的东西,但我不知道如何使用:[link][2]

我把最重要的代码放在这里,但我把完整的项目留给你,看看你是否愿意。希望你能帮我。我不确定是我做错了什么,还是问题出在其他方面。

【项目链接】[3]

主要.cpp

#include ... //some includes
using namespace std;
Shape* shape;
TableBoard board;
Square* square;
Command* command;
void keyDown(SDL_Event &evento) {
    if (evento.type == SDL_KEYDOWN) {
    SDL_Keycode key = evento.key.keysym.sym;
    command = nullptr;
    switch (key) {
        case SDLK_LEFT:
        command = new LeftRight(board, *shape, Direction::LEFT);
        shape->addCommand(command);
        break;
        case SDLK_RIGHT:
        command = new LeftRight(board, *shape, Direction::RIGHT);
        shape->addCommand(command);
        break;
        case SDLK_DOWN:
        command = new FallToGround(board, *shape);
        shape->addCommand(command);
        break;
        case SDLK_ESCAPE:
        exit(0);
    }
    }
}
void newShape() {
    if (shape == nullptr || !shape->isCanFall()) {
    shape = new LShape(*square);
    board.addShape(shape);
    shape->setCanFall(true);
    Command* command = new Fall(board, *shape);
    shape->addCommand(command);
    }
}
int main(int argc, char** argv) {
    Window pantalla;
    SDL_Event evento;
    board = TableBoard(pantalla.getWindow(), 20, 10);
    Board meassureBoard = board.getMeassureBoard();

    SDL_Texture* image = IMG_LoadTexture(pantalla.getPantalla(),
        "resources/images/blue_bold.png");
    //creo una celda, le paso datos de table y que él se pinte
    square = new Square();
    square = new Square();
    square->setGraphics(meassureBoard, image);
    square->setX(3);
    square->setY(1);

    bool letsQuit = false;
    Timer timer{};
    timer.start();
    newShape();
    while (!letsQuit) {
    pantalla.clear();
    shape->executeCommands();
    shape->clearCommandsFinished();
    board.paint(pantalla.getPantalla());
    board.drawLines(pantalla.getPantalla());
    pantalla.actualizar();
    while (SDL_PollEvent(&evento)) {
        if (evento.type == SDL_QUIT) { // Si es de salida
        letsQuit = true;
        } else {
        keyDown(evento);
        }
    }
    newShape();
    if (timer.delta() < 16) {
        SDL_Delay(16 - timer.delta());
    }
    timer.restart();
    }
    return 0;
}

形状的一部分(LShape的父类)

void Shape::executeCommands() {
    for(vector<Command*>::iterator it = commands.begin(); it != commands.end(); ++it) {
    (*it)->execute();
    }
}
void Shape::clearCommandsFinished() {
    vector<int> remove;
    int index=0;
    for(vector<Command*>::iterator it = commands.begin(); it != commands.end(); ++it) {
    if ((*it)->isAlive() != true) {
        remove.push_back(index);
    }
    index++;
    }
    for(vector<int>::iterator it = remove.begin(); it != remove.end(); ++it) {
        commands.erase(commands.begin()+(*it));
    }
}

Fall命令的一部分,左侧和右侧类似。这里给出错误

void Fall::execute() {
    if (isAlive() && timer.delta() >= milisecondsFalling) {
    timer.restart();
    bool canMove = true;
    vector<Square> squares = shape->nextMove(0, 1);
    if (shape->isCanFall()) {
        int number=shape->getNUMBER_OF_SQUARES();
        for (int i = 0; i < number && canMove; i++) {
        //check the range
        if (squares[i].getX() < 0
            || squares[i].getX() > board.getNumberColumns() - 1
            || squares[i].getY() < 0
            || squares[i].getY() > board.getNumberRows() - 1) {
            canMove = false;
        } else {
            if (board.isFilled(shape, squares[i])) {
            canMove = false;
            }
        }
        }
        //if we can move, move it definitively
        if (canMove) {
        shape->move(0, 1);
        board.updateBoard();
        }else{
        shape->setCanFall(false);
        }
    } else {
        alive = false;
    } //-> AT THIS EXACTLY MOMENT GIVE THE ERROR!! NOT AFTER, NOT BEFORE
    }
}

不重要,但这里也有错误。窗口类的部件

void Window::actualizar() {
    // Mostramos la pantalla
    SDL_RenderPresent(renderer); //EXACTLY POINT WHERE THROW THE ERROR, less frecuent
}
void Window::inicializacion() {
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    cout << "No se pudo iniciar SDL: " << SDL_GetError() << endl;
    exit(1);
    }
    atexit(SDL_Quit);
    pantalla = SDL_CreateWindow("Tetris",   //CREATED AS PRIVATE IN HEADER INSIDE CLASS: SDL_Window *pantalla;
              SDL_WINDOWPOS_UNDEFINED,
              SDL_WINDOWPOS_UNDEFINED,
              window.w, window.h,
              0);
    renderer = SDL_CreateRenderer(pantalla, -1, SDL_RENDERER_ACCELERATED);
    this->clear();
    if (pantalla == NULL) {
    cout << "Error iniciando el entorno gráfico: " << SDL_GetError() << endl;
    exit(1);
    }
    this->pantalla=pantalla;
}
Window::Window() {
    window.w=1024;
    window.h= 768;
    inicializacion();
}

编辑:链接已删除。问题已解决

不会去调试整个事情(这对我们任何一个人来说都是毫无成效的)。我对您的源库进行了简短的查看,发现了以下内容:

vector<Square> Shape::nextMove(int x, int y) 
{
    //make a copy to not really moving the squares
    vector<Square> clone(numberSquares);
    for (int i = 0; i <= numberSquares; i++) { // <<=== HERE
        clone[i] = *(new Square(squares[i]));
    }
    //get the next move
    moveShape(clone, x, y);
    //return the modified squares
    return clone;
}

<=的索引超过了源向量和目标向量的大小,即BAD。更糟糕的是,for循环中的代码是公然的内存泄漏。整个功能应该简化为:

vector<Square> Shape::nextMove(int x, int y) 
{
    vector<Square> clone = squares;
    moveShape(clone, x, y);
    return clone;
}

祝好运