简体   繁体   中英

_CrtisValidHeapPointer(block) error using SetConsoleCursorPosition function

Develop console Snake. At the end of the program gives an error _CrtisValidHeapPointer(block). Found out by experience, that the problem is in SetConsoleCursorPosition function in line 32

#include <iostream>
#include <Windows.h>
#include "Drawer.h"
#include "Snake.h"
 
using namespace std;
 
int main()
{
    Drawer BasicImage;// создание объекта класса основной картинки игры
    Snake Player;
 
    short pre_x_pos,
          pre_y_pos;
 
    bool game_over = false;
 
    HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE);
 
    BasicImage.ShowConsoleCursor(false);
    BasicImage.FormField();
    SetConsoleCursorPosition(console_handle, { 0, 0 });
    BasicImage.FormWalls();
 
    while (!game_over)
    {
        Player.MoveSet();
 
        pre_x_pos = Player.x_pos;
        pre_y_pos = Player.y_pos;
        
        SetConsoleCursorPosition(console_handle, { pre_x_pos, pre_y_pos });
        cout << BasicImage.floor_skin;
        SetConsoleCursorPosition(console_handle, { Player.x_pos, Player.y_pos });
        cout << Player.snake_skin;
 
        if (BasicImage.field[Player.x_pos][Player.y_pos] == BasicImage.wall_skin)
        {
            game_over = true;
        }
 
        Sleep(500);
    }
 
 
    SetConsoleCursorPosition(console_handle, { 0, BasicImage.height });
    cout << "Game Over";
 
    return 0;
}

The compiler doesn't make any error message,I can easily launch the app, but when I close the main window,_CrtIsValidHeapPointer(block) error was raised

Previously, 'pre_x_pos' and 'pre_y_pos' was variables belonging to the 'Snake' class. I tried to move it to main file, but these solution didn't help.

UPD: content of all other files: Drawer.h:

class Drawer
{
public:
    short height = 0,
          length = 0;

    char wall_skin = '#';
    char floor_skin = '.';

    char** field = 0;

    void ShowConsoleCursor(bool);

    void FormField();

    void FormWalls();
};

Snake.h:

class Snake
{
public: 
    short x_pos = 1,
          y_pos = 4,
          pre_x_pos = 0,
          pre_y_pos = 0,
          x_scale = 0,
          y_scale = 0,
          last_dir = 0;

    char snake_skin = '@';

    void MoveSet();

    void MoveLeft();

    void MoveRight();

    void MoveUP();

    void MoveDown();

};

Drawer.cpp:

#include <iostream>
#include <Windows.h>
#include "Drawer.h"

using namespace std;

void Drawer::ShowConsoleCursor(bool show_flag)
{
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO     cursorInfo;

    GetConsoleCursorInfo(out, &cursorInfo);
    cursorInfo.bVisible = show_flag; // set the cursor visibility
    SetConsoleCursorInfo(out, &cursorInfo);
}

void Drawer::FormField()
{
    cin >> height;
    cin >> length;

    field = new char*[height];

    for (int i = 0; i < height; i++)
    {
        field[i] = new char;
    }

}

void Drawer::FormWalls()
{
    
    for (int i = 0; i < height; i++)
    {

        for (int j = 0; j < length; j++)
        {

            if (i == 0 || i == height - 1 || j == 0 || j == length - 1)
            {
                field[i][j] = wall_skin;
                cout << field[i][j];
            }

            else
            {
                field[i][j] = floor_skin;
                cout << field[i][j];
            }

        }

        cout << endl;
    }

}

Snake.cpp:

#include <Windows.h>
#include "Snake.h"

void Snake::MoveSet()
{
    if (GetAsyncKeyState(0x57) && last_dir != 3)
    {
        Snake::MoveUP();
        last_dir = 1;
    }

    else if (GetAsyncKeyState(0x44) && last_dir != 4)
    {
        Snake::MoveRight();
        last_dir = 2;
    }

    else if (GetAsyncKeyState(0x53) && last_dir != 1)
    {
        Snake::MoveDown();
        last_dir = 3;
    }

    else if (GetAsyncKeyState(0x41) && last_dir != 2)
    {
        Snake::MoveLeft();
        last_dir = 4;
    }

    x_pos += x_scale;
    y_pos += y_scale;
}

void Snake::MoveDown()
{
    x_scale = 0;
    y_scale = 1;
}

void Snake::MoveRight()
{
    x_scale = 1;
    y_scale = 0;
}

void Snake::MoveLeft()
{
    x_scale = -1;
    y_scale = 0;
}

void Snake::MoveUP()
{
    x_scale = 0;
    y_scale = -1;
}

I made a mistake when allocating memory:

void Drawer::FormField()
{
cin >> height;
cin >> length;

field = new char*[height];

for (int i = 0; i < height; i++)
{
    field[i] = new char[];
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM