0

I'm creating a simple Windows Console application and am experiencing a strange error. The program runs fine and does everything without error, but when it hits the final return 0; in the main routine i get the following error

https://i.imgur.com/A0Veq2Y.png

enter image description here

I've never seen it before and don't know what it means. I tried to read through the documentation but it was not enlightening.

Here's the program. I do some dynamic allocations, so I thought that might be it but I've never gotten this error in the past despite having made plenty of pointer, dynamic allocation, and indexing errors before. It's always seg-faults, syntax, or compilation errors.

const char WHITE = 'x';
const char BLACK = 'o';
const char EMPTY = '-';
const char EDGE = 'e';

struct BoardSpace
{
    int x_loc;
    int y_loc;
    int white_score;
    int black_score;
    string last_visitor;
    char stone;
    char owner;
};

class GoBoard
{

private:

    void InitBoard(BoardSpace** board)
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            board[x][0].stone = EDGE;
            board[0][x].stone = EDGE;
            board[x][BoardSize - 1].stone = EDGE;
            board[BoardSize - 1][x].stone = EDGE;
        }

        for (unsigned int x = 1; x < BoardSize - 1; x++)
        {
            for (unsigned int y = 1; y < BoardSize - 1; y++)
            {
                MyBoard[x][y].stone = EMPTY;
            }
        }
    }

public:

    unsigned int BoardSize;
    BoardSpace **MyBoard;

    GoBoard(int size)
    {
        BoardSize = size + 2;
        MyBoard = new BoardSpace*[BoardSize];

        for (unsigned int x = 0; x < BoardSize; x++)
        {
            MyBoard[x] = new BoardSpace[BoardSize];
        }

        InitBoard(MyBoard);
    }

    ~GoBoard()
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            delete MyBoard[x];
        }

        delete MyBoard;
    }

    void PrintBoard()
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            for (unsigned int y = 0; y < BoardSize; y++)
            {
                cout << MyBoard[x][y].stone << ' ';
            }
            cout << endl;
        }
    }
};
user3776749
  • 667
  • 1
  • 10
  • 20
  • 4
    `delete MyBoard;` is wrong. It needs to be `delete [] MyBoard;` – R Sahu May 26 '19 at 06:22
  • Did you implement move or copy constructor? – JVApen May 26 '19 at 06:28
  • Please show a [mcve], you have memory corruption somewhere – Alan Birtles May 26 '19 at 06:31
  • 1
    For starters, `GoBoard MyGoBoard(9);` Not that it necessarily matters, but this isn't Java. – WhozCraig May 26 '19 at 06:31
  • 2
    Rule-of-three violation, the pointers get deleted twice. Let the compiler help you catch accidental copying with `= delete` on the copy constructor and assignment operator. – Hans Passant May 26 '19 at 06:32
  • Your not vector-deleting either of your vector-allocations. Both `delete [] MyBoard[x];` and `delete [] MyBoard;` are required. Fyi, though this is ripe for RO3 violation you actually don't have one (yet). The thing crashing your program is not using `delete [] MyBoard[x];` on the inside vector destructions. If you fix both missing vector-syntax markers on your `delete` invokes this will work (for now, anyway). – WhozCraig May 26 '19 at 06:44
  • 1
    Possible duplicate of [Is delete\[\] equal to delete?](https://stackoverflow.com/questions/1553382/is-delete-equal-to-delete) – πάντα ῥεῖ May 26 '19 at 07:06

1 Answers1

1

As mentioned in the comments, the dtor must change to:

~GoBoard()
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            delete [] MyBoard[x];
        }

        delete [] MyBoard;
    }

however it is recommended to use smart pointers or other containers like vector instead. Here is an example with uinque_ptr:

#include <iostream>
#include <string>
#include<memory>
using namespace std;

const char WHITE = 'x';
const char BLACK = 'o';
const char EMPTY = '-';
const char EDGE = 'e';

struct BoardSpace
{
    int x_loc;
    int y_loc;
    int white_score;
    int black_score;
    string last_visitor;
    char stone;
    char owner;
};

class GoBoard
{

private:

    void InitBoard(unique_ptr<unique_ptr<BoardSpace[]>[]>& board)
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            board[x][0].stone = EDGE;
            board[0][x].stone = EDGE;
            board[x][BoardSize - 1].stone = EDGE;
            board[BoardSize - 1][x].stone = EDGE;
        }

        for (unsigned int x = 1; x < BoardSize - 1; x++)
        {
            for (unsigned int y = 1; y < BoardSize - 1; y++)
            {
                MyBoard[x][y].stone = EMPTY;
            }
        }
    }

public:

    unsigned int BoardSize;
    unique_ptr<unique_ptr<BoardSpace[]>[]> MyBoard;

    GoBoard(int size)
    {
        BoardSize = size + 2;
        MyBoard.reset(new unique_ptr<BoardSpace[]>[BoardSize]);

        for (unsigned int x = 0; x < BoardSize; x++)
        {
            MyBoard[x].reset(new BoardSpace[BoardSize]);
        }

        InitBoard(MyBoard);
    }

    ~GoBoard()
    {
    }

    void PrintBoard()
    {
        for (unsigned int x = 0; x < BoardSize; x++)
        {
            for (unsigned int y = 0; y < BoardSize; y++)
            {
                cout << MyBoard[x][y].stone << ' ';
            }
            cout << endl;
        }
    }
};

int main()
{
    GoBoard myGboard(9);
    myGboard.PrintBoard();
    return 0;
}
Oblivion
  • 7,176
  • 2
  • 14
  • 33
  • 1
    I have no idea why this was down-voted. If the OP takes the advice from general-comment of deleting the default copy-ctor and copy-assignment operator in order to "Let the compiler help you catch accidental copying" it would lead to *nothing*. Although a proper practice to shore up possible R03 violations, there aren't any in the posted code. Even adding deleted specs on those members, the code still compiles, and still crashes. The real culprit is *exactly* what you showed here; the improper firing of `delete` where `delete []` should be used. Ideally, the OP fixes *both* problems. – WhozCraig May 27 '19 at 04:45