user3776749
user3776749

Reputation: 697

Visual Studio Windows Console Application throwing exception on 'return 0' from main()

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

Upvotes: 0

Views: 119

Answers (1)

Oblivion
Oblivion

Reputation: 7374

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

Upvotes: 1

Related Questions