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

标签 c++ memory vector sdl-2

我知道这个问题在 stackoverflow 中有答案,但我仍然无法解决我的问题,我没有看到错误。我正在使用 vector ,并且我认为我使用正确,我没有做任何 free 或 malloc/calloc 之类的事情。但是,我确信问题出在 vector 上,做错了什么。 最奇怪的部分是调试程序,它发生在两三个不同的站点。

我使用 SDL2 和 Linux 上的默认 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]

main.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;
}

Shape 的一部分(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;
}

那个<=索引的大小超过源 vector 和目标 vector 的大小,这是不好的。更糟糕的是,for 循环中的代码存在明显的内存泄漏。整个函数应该简化为:

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

祝你好运

关于C++:损坏的双链表和内存损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877299/

相关文章:

linux - "used memory"高,但似乎不准确 - ksysguardd 与 htop 相比?

windows - Octave - 内存不足或维度对于 Octave 的索引类型来说太大

ios - 跟踪分配/内存使用情况并逐步执行 XCode 中的代码?

c++ - 在到达新行之前如何运行 while 循环?

c++ - 标准 vector push_back' : 2 overloads have no legal conversion for 'this' pointer error

c++ - 更好的实现策略是什么?

c++ - 具有 get 函数的模板类总是返回引用,

c++ - 从无序关联容器中移动对象

c++ - Qt 将几何图形设置为新创建的小部件

c++ - Tab 到 QTableView 中的下一个可见列