   Crash When Minimized DirectX
  My DirectX program crashes when I minimize it or OVERLAP another window over-top of it. I get the error: "Unhandled exception at 0x61a8b0e9 in Wumpus World.exe: 0xC0000005: Access violation writing location 0x7602300f." My game is a 2D AI simulation that uses 2D animation sprites. Any help would be greatly appreciated. Thank you! PS. Here are a few of my more-important functions:#include "global.h"LPDIRECT3D9 d3d = NULL;LPDIRECT3DDEVICE9 d3ddev = NULL;D3DPRESENT_PARAMETERS d3dpp;LPD3DXSPRITE d3dspt;HWND hWnd = NULL;SPRITE mainMenu_SPRITE;SPRITE mainMenuArrow_SPRITE;//PlayerSPRITE player_SPRITE;//board spriteSPRITE background_SPRITE;//Wumpus spriteSPRITE wumpus_SPRITE;//Black Hole spritesSPRITE blackHole_SPRITE;SPRITE blackHole2_SPRITE;SPRITE gold_SPRITE;//5 by 5 game board 25 spaces totalSPRITE gameBoard_SPRITE[maxRows][maxCols];//stench for WumpusSPRITE stench_SPRITE;//gravity for black holeSPRITE gravity_SPRITE;SPRITE gravity2_SPRITE;int GetPlayerXPosition(){return player_SPRITE.xPosInCords;}int GetPlayerYPosition(){return player_SPRITE.yPosInCords;}// Create Direct3D and the Direct3D Devicevoid InitDirect3D(GAMEWINDOW* gw){if(FAILED(d3d = Direct3DCreate9(D3D_SDK_VERSION))){MessageBox(NULL, "The Direct3DCreate9 function failed", "Error", MB_OK);PostQuitMessage(0);}ZeroMemory(&d3dpp, sizeof(d3dpp));d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;d3dpp.Windowed = gw->Windowed;d3dpp.BackBufferWidth = gw->Width;d3dpp.BackBufferHeight = gw->Height;d3dpp.EnableAutoDepthStencil = TRUE;d3dpp.AutoDepthStencilFormat = D3DFMT_D16;//check to see if the graphics card device fails then start using the processor insteadif(FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_HARDWARE_VERTEXPROCESSING,&d3dpp,&d3ddev))){MessageBox(NULL, "The CreateDevice function failed", "Error", MB_OK);PostQuitMessage(0);}d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);    // turn off the 3D lighting//d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    // turn on the z-bufferD3DXCreateSprite(d3ddev, &d3dspt);}/* Inits each space to the proper x and y to match the game board if the board is moved or changed this must be adjusted as well */void InitBoard(){int xPosInPixels = 0;int yPosInPixels = 0;//Init each spacefor (int y = 0; y < maxRows; y++){for (int x = 0; x < maxCols; x++){//store x and y in pixels for each spacexPosInPixels = ((x * 100) + 160);yPosInPixels = ((y * 100) + 90);gameBoard_SPRITE[x][y].xPosInPixels = xPosInPixels;gameBoard_SPRITE[x][y].yPosInPixels = yPosInPixels;//store x and y in cord's as wellgameBoard_SPRITE[x][y].xPosInCords = x;gameBoard_SPRITE[x][y].yPosInCords = y;}}}//Place stench around Wumpusvoid PlaceStench(int x, int y){if (wumpus_SPRITE.isAlive == true){if (x > 0){gameBoard_SPRITE[x - 1][y].isStench = true;}if (x < 4){gameBoard_SPRITE[x + 1][y].isStench = true;}if (y > 0){gameBoard_SPRITE[x][y - 1].isStench = true;}if (y < 4){gameBoard_SPRITE[x][y + 1].isStench = true;}}}void PlaceBreeze(int x, int y){//place gravityif (x > 0){gameBoard_SPRITE[x - 1][y].isBreeze = true;}if (x < 4){gameBoard_SPRITE[x + 1][y].isBreeze = true;}if (y > 0){gameBoard_SPRITE[x][y - 1].isBreeze = true;}if (y < 4){gameBoard_SPRITE[x][y + 1].isBreeze = true;}}//Randomly place Wumpus on the boardvoid PlaceWumpus(){if (wumpus_SPRITE.isAlive == false){//place Wumpus (Not on player's position (0, 4))int x = ((rand() % 4) + 1);int y = (rand() % 4);//convert board space cords to pixelswumpus_SPRITE.xPosInPixels = ((x * 100) + 160);wumpus_SPRITE.yPosInPixels = ((y * 100) + 90);//store board space in cordswumpus_SPRITE.xPosInCords = x;wumpus_SPRITE.yPosInCords = y;gameBoard_SPRITE[x][y].isWumbus = true;wumpus_SPRITE.isAlive = true;PlaceStench(x,y);}}void PlaceBlackHoles(){//place Black Hole (Not on player's position (0, 4))int x = ((rand() % 4) + 1);int y = (rand() % 4);//Blackhole 2int x2 = ((rand() % 4) + 1);int y2 = (rand() % 4);//convert board space to pixels for black holeblackHole_SPRITE.xPosInPixels = ((x * 100) + 160);blackHole_SPRITE.yPosInPixels = ((y * 100) + 90);//convert board space to pixels for black holeblackHole2_SPRITE.xPosInPixels = ((x2 * 100) + 160);blackHole2_SPRITE.yPosInPixels = ((y2 * 100) + 90);//store cords for black holeblackHole_SPRITE.xPosInCords = x;blackHole_SPRITE.yPosInCords = y;//store cords for black holeblackHole2_SPRITE.xPosInCords = x2;blackHole2_SPRITE.yPosInCords = y2;//flag space to true for black holegameBoard_SPRITE[x][y].isblackHole = true;gameBoard_SPRITE[x2][y2].isblackHole = true;PlaceBreeze(x,y);PlaceBreeze(x2,y2);}//randomly place gold chest on boardvoid PlaceGold(){int x = ((rand() % 4) + 1);int y = (rand() % 4);//convert board space cords to pixelsgold_SPRITE.xPosInPixels = ((x * 100) + 160);gold_SPRITE.yPosInPixels = ((y * 100) + 90);//store cordsgold_SPRITE.xPosInCords = x;gold_SPRITE.yPosInCords = y;}//draw gold on screen until taken by playervoid IsGoldTaken(){//if gold has not been taken draw itif (player_SPRITE.foundGold == false){DrawSprite(&gold_SPRITE, 0, gold_SPRITE.xPosInPixels, gold_SPRITE.yPosInPixels, 0);if (GetPlayerXPosition() == gold_SPRITE.xPosInCords && GetPlayerYPosition() == gold_SPRITE.yPosInCords)//stop drawing goldplayer_SPRITE.foundGold = true;}}// Close the Device and Direct3Dvoid CloseDirect3D(){d3ddev->Release();d3d->Release();}void StartRender(){d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);d3ddev->Clear(0, NULL, D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);d3ddev->BeginScene();d3dspt->Begin(D3DXSPRITE_ALPHABLEND);}void EndRender(){d3dspt->End();d3ddev->EndScene();d3ddev->Present(NULL, NULL, NULL, NULL);}void ResetDevice(){if(d3ddev)d3ddev->Reset(&d3dpp);}// Load a graphic into a SPRITE objectvoid LoadSprite(SPRITE* pSprite, LPCTSTR File, int width, int height, int cols, int rows){if (d3ddev != NULL && pSprite != NULL)D3DXCreateTextureFromFileEx(d3ddev,File,D3DX_DEFAULT,D3DX_DEFAULT,D3DX_DEFAULT,NULL,D3DFMT_A8R8G8B8,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,D3DCOLOR_XRGB(255, 255, 255),NULL,NULL,&pSprite->tex);pSprite->width = width;pSprite->height = height;pSprite->cols = cols;pSprite->rows = rows;}// Draw a frame from a SPRITE objectvoid DrawSprite(SPRITE* pSprite, int Frame, float x, float y, float z){RECT FrameBox;FrameBox.left = (Frame % pSprite->cols) * pSprite->width;FrameBox.top = (Frame / pSprite->cols) * pSprite->height;FrameBox.right = FrameBox.left + pSprite->width;FrameBox.bottom = FrameBox.top + pSprite->height;D3DXVECTOR3 position(x, y, z);d3dspt->Draw(pSprite->tex, NULL, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));}// Draw a frame from a animated SPRITE objectvoid DrawAnimSprite(SPRITE* pSprite, int Frame, float x, float y, float z){RECT FrameBox;FrameBox.left = (Frame % pSprite->cols) * pSprite->width;FrameBox.top = (Frame / pSprite->cols) * pSprite->height;FrameBox.right = FrameBox.left + pSprite->width;FrameBox.bottom = FrameBox.top + pSprite->height;D3DXVECTOR3 position(x, y, z);d3dspt->Draw(pSprite->tex, &FrameBox, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));}//place the players starting pos in the stackvoid InitPlayer(){AddSpaceToStack();player_SPRITE.isAlive = true;}//backtrack stepsvoid BackTrackSteps(){//make sure stack is not emptyif (!player_SPRITE.prevXCords_STACK.empty()){//remove old position from stack pixels player_SPRITE.prevXPixels_STACK.pop();player_SPRITE.prevYPixels_STACK.pop();//remove old position from stack cordsplayer_SPRITE.prevXCords_STACK.pop();player_SPRITE.prevYCords_STACK.pop();//cords for game board not in 5x5 cordsplayer_SPRITE.xPosInCords = player_SPRITE.prevXCords_STACK.getTOS();player_SPRITE.yPosInCords = player_SPRITE.prevYCords_STACK.getTOS();//start returning to starting pos pixels by back-tracking old steps in stackplayer_SPRITE.xPosInPixels = player_SPRITE.prevXPixels_STACK.getTOS();player_SPRITE.yPosInPixels = player_SPRITE.prevYPixels_STACK.getTOS();}elseMoveRandomDirection();}//if gold found return homevoid ReturnToStart(){//check for home plateif (GetPlayerXPosition() != 0 || GetPlayerYPosition() != 4)BackTrackSteps();}//start moving in a random direction //ensure space is not dangerous and not a wallvoid MoveRandomDirection(){int direction = rand() % 4;switch (direction){case 0:{//leftif (GetPlayerXPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition() - 1][GetPlayerYPosition()].wumpusDangerPercent == 0){player_SPRITE.xPosInPixels -= 100;player_SPRITE.xPosInCords -= 1;AddSpaceToStack();}break;}case 1:{//rightif (GetPlayerXPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition() + 1][GetPlayerYPosition()].wumpusDangerPercent == 0){player_SPRITE.xPosInPixels += 100;player_SPRITE.xPosInCords += 1;AddSpaceToStack();}break;}case 2:{//downif (GetPlayerYPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() + 1].wumpusDangerPercent == 0){player_SPRITE.yPosInPixels += 100;player_SPRITE.yPosInCords += 1;AddSpaceToStack();}break;}case 3:{//upif (GetPlayerYPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() - 1].wumpusDangerPercent == 0){player_SPRITE.yPosInCords -= 1;player_SPRITE.yPosInPixels -= 100;AddSpaceToStack();}break;}}}/*fuzzy logic for detecting the Wumpusif space has NOT been visited and space is stenchthen add 33 to surrounding non visited non wall spaces*/void RuleFourFuzzyLogicWumpus(){if (wumpus_SPRITE.isAlive == true){//leftif (GetPlayerXPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition() - 1][GetPlayerYPosition()].isVisited == false){gameBoard_SPRITE[GetPlayerXPosition() - 1][GetPlayerYPosition()].wumpusDangerPercent += 33;}//rightif (GetPlayerXPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition() + 1][GetPlayerYPosition()].isVisited == false){gameBoard_SPRITE[GetPlayerXPosition() + 1][GetPlayerYPosition()].wumpusDangerPercent += 33;}//downif (GetPlayerYPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() + 1].isVisited == false){gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() + 1].wumpusDangerPercent += 33;}//upif (GetPlayerYPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() - 1].isVisited == false){gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() - 1].wumpusDangerPercent += 33;}}}//check spaces around the character for a 50 or greater % chance of being the wumpusvoid AttemptKillWumpus(){if (wumpus_SPRITE.isAlive == true){//leftif (GetPlayerXPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition() - 1][GetPlayerYPosition()].wumpusDangerPercent > 60){DeleteStenchAndWumpusDanger();}//rightif (GetPlayerXPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition() + 1][GetPlayerYPosition()].wumpusDangerPercent > 60){DeleteStenchAndWumpusDanger();}//downif (GetPlayerYPosition() < 4 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() + 1].wumpusDangerPercent > 60){DeleteStenchAndWumpusDanger();}//upif (GetPlayerYPosition() > 0 && gameBoard_SPRITE[GetPlayerXPosition()][GetPlayerYPosition() - 1].wumpusDangerPercent > 60){DeleteStenchAndWumpusDanger();}}}//add player steps to stack because AI remembers his stepsvoid AddSpaceToStack(){player_SPRITE.prevXPixels_STACK.push(player_SPRITE.xPosInPixels);player_SPRITE.prevYPixels_STACK.push(player_SPRITE.yPosInPixels);player_SPRITE.prevXCords_STACK.push(GetPlayerXPosition());player_SPRITE.prevYCords_STACK.push(GetPlayerYPosition());}//If Wumpus is dead delete stench and danger wumpusvoid DeleteStenchAndWumpusDanger(){if (wumpus_SPRITE.isAlive == true){for (int i = 0; i < maxCols; i++){for (int x = 0; x < maxRows; x++){gameBoard_SPRITE[x].isStench = false;gameBoard_SPRITE[x].wumpusDangerPercent = 0;gameBoard_SPRITE[x].isWumbus = false;}}wumpus_SPRITE.isAlive = false;}}
  Looks like it might be an unitialized or otherwise invalid variable. If you can reliably reproduce the problem, and if you're running Visual Studio: when it breaks on the error, open the disassembly window and look at the code at 0x61... If that isn't the exact line of code, it should be close enough you can add some debug statements to your code to determine where you're writing something invalid.   ;You should set HWND of window in presentation structure as well (hDeviceWindow member),by the way I do not see in code where you actualy get window handle,you initialize hwnd at NULL and pass it right into CreateDevice(), if you don't specify the window handle you can run only in fullscreen mode, specify window handle and you should be ok in windowed mode as well.   