Heap corruption when deleting global array

The following code seems to be causing heap corruption whenever the debugger hits the delete[] operator. It's trying to delete a global array of structures which was defined as an extern in the header file and then declared in the global scope of the main .cpp file.

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

I've searched various heap corruptions topics on forums the last couple of days and so far have come up dry trying various things.

One person suggested that maybe the pointer to my array tile_array had changed by the time the program hits delete[]. But I've checked this the a couple of times and the address of both the pointer and the first element of the array remain the same.

Other forum answer from other people's posts usually suggest that I may be deleting the array more than once, but so far this is the only time the delete[] operator is used for that specific array.

Answers


Without knowing every access to the title_array it is hard to speculate on the cause of this problem. With memory corruption where the program shows up isn't always what caused it.

One thing that you may want to check is that the extern declaration is for a "tile* tile_array " and not "tile tile_array []". I very much so doubt this is the case, but when dealing with extern and arrays I have seen this mistake bite a few people.

Another thing I would do is make sure to NULL out tile_array after deleting it and when you declare it. If the problem goes away when you do this then it was in fact either a too early delete (before it was initialized) or too many deletes.

The last thing you will want to double check is that there are no buffer over/under-runs. The new[] operator places some metadata on the heap that the delete[] operator uses to know the size of the array so it can properly cleanup the memory. Corrupting this metadata can result in heap corruption.


Need Your Help

Prototyping My Own Desktop Environment

prototype user-interface operating-system platform

I have an idea for a new desktop environment that involves a radically different user interface than existing ones, and I want to begin prototyping it. However, I don't want to go through all the t...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.