Note that there are some explanatory texts on larger screens.

plurals
  1. POHeap corruption when deleting global array
    primarykey
    data
    text
    <p>The following code seems to be causing heap corruption whenever the debugger hits the <code>delete[]</code> operator. It's trying to delete a global array of structures which was defined as an <code>extern</code> in the header file and then declared in the global scope of the main .cpp file.</p> <pre><code>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; } </code></pre> <p>I've searched various heap corruptions topics on forums the last couple of days and so far have come up dry trying various things.</p> <p>One person suggested that maybe the pointer to my array tile_array had changed by the time the program hits <code>delete[]</code>. 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.</p> <p>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 <code>delete[]</code> operator is used for that specific array.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload