D3D11CreateDeviceAndSwapChain doesnt seem to assign addresses to Swap Chain or Device?

Started by
1 comment, last by Aaron7 12 years, 10 months ago
Hello Everyone,

I'm very new to DirectX, so I'm having a hard time figuring out a problem in the code I'm looking at now. When I run the following code, it compiles, but when I close the window I get a message saying "Unhandled exception at 0x012f1873 in Window2.exe: 0xC0000005: Access violation reading location 0x00000000." When I went through debugging, my SwapChain, Device, and DeviceContext were all 0x00000000. That message pops up on SwapChain->Release() (I'm assuming it'd come up on the other two as well if it got to them).

I sent the code to a friend of mine, who compiled and ran it without any problem... the pointers all had valid memory addresses. We have the same version of the DirectX sdk. Originally I was compiling in Visual Studio 2010, but I just tried again with 2008 (which is what he was working with) and I got the same problem.

I noticed when D3D11CreateDeviceAndSwapChain was called, I got three messages in the debugger saying "First-chance exception at 0x7588b727 in Window2.exe: Microsoft C++ exception: _com_error at memory location 0x0034f388..", with a different address each time. Not sure if that has anything to do with it, just throwing it out there in case :)

Here's the code, any ideas why the pointers aren't being assigned to?


//..........................................................................................................//
//............................................Includes......................................................//
//..........................................................................................................//

//Windows Includes
#include <Windows.h>
#include <WindowsX.h>

//DirectX Includes
#include <D3D11.h>
#include <D3DX11.h>
#include <D3DX10.h>

//..........................................................................................................//
//............................................Pragmas.......................................................//
//..........................................................................................................//

//DirectX libraries
#pragma comment (lib, "D3D11.lib")
#pragma comment (lib, "D3DX11.lib")
#pragma comment (lib, "D3DX10.lib")

//..........................................................................................................//
//............................................Globals.......................................................//
//..........................................................................................................//

//DirectX Globals
IDXGISwapChain* SwapChain;
ID3D11Device* Device;
ID3D11DeviceContext* DeviceContext;

//..........................................................................................................//
//......................................Function Protoytypes................................................//
//..........................................................................................................//

//Windows functions
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

//DirectX functions
void InitD3D(HWND hWnd);
void CleanD3D();

//..........................................................................................................//
//.....................................Function Definitions.................................................//
//..........................................................................................................//

////////////////////////////////////////////
//WinMain: The entry point for our program//
////////////////////////////////////////////
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
//Variable Declarations
HWND hWnd;
WNDCLASSEX wc;

//Set up Window Class
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hInstance = hInstance;
wc.lpfnWndProc = WinProc;
wc.lpszClassName = L"WindowClass1";
wc.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&wc);

//Create and show window
RECT rect = {0, 0, 600, 400};
AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, false);
hWnd = CreateWindowEx(NULL, L"WindowClass1", L"Window Title", WS_OVERLAPPEDWINDOW, 300, 300, rect.right - rect.left,
rect.bottom - rect.top, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);

//Initialize DirectX 3D
InitD3D(hWnd);

//Game Loop
MSG msg;
while(true)
{
//Respond to user input if provided
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (msg.message == WM_QUIT)
break;
}
//Go about rest of game's business here
}

//Release DirectX 3D
CleanD3D();

//Return msg's wParam when finished
return msg.wParam;
}

/////////////////////////////////////
//WinProc: Respond to user messages//
/////////////////////////////////////
LRESULT CALLBACK WinProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch(message)
{
case(WM_DESTROY):
PostQuitMessage(0);
return 0;
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
break;
}
}

//////////////////////////////////
//InitD3D: Initialize DirectX 3D//
//////////////////////////////////
void InitD3D(HWND hWnd)
{
//Variable Declarations
DXGI_SWAP_CHAIN_DESC SwapChainDesc;

//Setup Swap Chain Description
ZeroMemory(&SwapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));
SwapChainDesc.BufferCount = 1;
SwapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
SwapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
SwapChainDesc.OutputWindow = hWnd;
SwapChainDesc.SampleDesc.Count = 4;
SwapChainDesc.Windowed = true;

//Create Device and Swap Chain from description above
D3D11CreateDeviceAndSwapChain(NULL, D3D_DRIVER_TYPE_HARDWARE, NULL, NULL, NULL, NULL, D3D11_SDK_VERSION, &SwapChainDesc,
&SwapChain, &Device, NULL, &DeviceContext);
}

//////////////////////////////////////////////////////
//CleanD3D: Release memory for Swap Chain and Device//
//////////////////////////////////////////////////////
void CleanD3D()
{
SwapChain->Release();
Device->Release();
DeviceContext->Release();
}
Advertisement
Check the HRESULT returned by D3D11CreateDeviceAndSwapChain in case it is failing, Also passing D3D11_CREATE_DEVICE_DEBUG to the flags parameter may give more information in the output about what is going wrong

Check the HRESULT returned by D3D11CreateDeviceAndSwapChain in case it is failing, Also passing D3D11_CREATE_DEVICE_DEBUG to the flags parameter may give more information in the output about what is going wrong


Thanks! Looks like the SampleDesc.Count was too high for my computer, turning it down from 4 to 1 got it working.

This topic is closed to new replies.

Advertisement