Deleting global array of struct causing heap corruption

I'm working on a Minesweeper clone to get practice on Win32. I have a global array of structs called tile_array. This is an array of structs called "tile." Each tile struct contains the x coordinate, y coordinate, whether there's a mine in the tile, whether there's a mine next to it, etc.

First, I've declared this in my header file so various different .cpp files in the Solution can use it. It's defined as an extern.

extern tile *tile_array;

Then, it's declared again at the to of my main .cpp file.

tile *tile_array = NULL;

After the user has inputted how many tiles they want horizontal and vertical, and how many mines will be on the field, the total tiles needed are calculated and the memory is allocated based on the total tiles. (For purposes of testing, the horizontal, vertical, and mine count are temporarily hardcoded.)

1
2
3
4
5
6
7
8
	tiles_horiz = 10;  //temp code
	tiles_vert = 10;   //temp code
	num_mines = 5;    //temp code

        ...

	tiles_total = (tiles_horiz * tiles_vert);
	tile_array = new tile[tiles_total];


Whenever I delete tile_array using the delete[] operator, I get a heap corruption error from Visual Studio. It doesn't say that's the error specifically, but it forces a break in the debugger and the problem is always in free.c

Right now I have the delete operator placed toward the end of the program when it's ready to exit. But my intention is to use it if the player resets, too, so that the tile_array array and be slept clean and reset based on any new selections.
I can only suppose that maybe somewhere in the code you change the pointer. For example maybe you use an increment operation with pointer as for example

tile_array++;

or

tile_array += 1; (or any other value).

Check your code.
Last edited on
I've checked a few times and that doesn't seem to be the case. I've set debug points right after where the memory is allocated with the new[] keyword, and then also right at and after the delete[]. At all three breakpoints, the address for the pointer remains the same.
It could be that you overwrote the memory that is before the array. Can it be that your structure contains pointers as its members? Show your structure tile.
Last edited on
Sure, here's the struct tile:

1
2
3
4
5
static struct tile{
	int x, y, mine, proximity;
	int up_lt, up, up_rt, lt, rt, dn_lt, dn, dn_rt;
	int left_edge, right_edge;
};


And if it helps, here's the entire window procedure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		case WM_CREATE:
		{
			int main_win_x, main_win_y;

			tiles_horiz = 10;  //temp code
			tiles_vert = 10;   //temp code
			num_mines = 5;    //temp code

			main_win_x = (tiles_horiz * 22) + 20;
			main_win_y = (tiles_vert * 22) + 20;

			MoveWindow(hwnd, 100, 100, main_win_x, main_win_y, TRUE);

			tiles_total = (tiles_horiz * tiles_vert);
			tile_array = new tile[tiles_total];

			SetupPlayField();
			DrawInitTiles(hwnd);
		}
		break;
		case WM_SIZE:
		{

		}
		break;
		case WM_CLOSE:
			delete[] tile_array;
			DestroyWindow(hwnd);
		break;
		case WM_DESTROY:
			PostQuitMessage(0);
		break;
		default:
			return DefWindowProc(hwnd, msg, wParam, lParam);
	}
	return 0;
}
Try to insert here

1
2
3
4
		case WM_CLOSE:
			delete[] tile_array;
			tile_array = NULL;
			DestroyWindow(hwnd);
Thanks for your help in this, vlad. That code above still reported a heap corruption. However, if I set tile_array to NULL before the delete[] operator, I don't get the heap corruption. Although that also seems to make the delete[] operator a bit pointless from examining the memory. Because in the debugger it looks like everything's set back to an uninitialized value (all members of all structs in the array have a value of "???") and delete[] doesn't seem to have any effect.

Another thing that I noticed is if I use a constant when allocating tile_array instead of the variable tiles_total, then I don't get heap corruption anywhere and the delete[] operator works flawlessly without tile_array being set to NULL beforehand. Of course a constant won't work for what I'm trying to do, though. So I guess I'm stuck with setting the array to NULL for now.
Topic archived. No new replies allowed.