Unhandled exception when closing directx

Started by
4 comments, last by Adam_42 12 years, 2 months ago
[color=#434343][font=helvetica, arial, verdana,]I am learning directx from [/font]directxtutorial.com[color=#434343][font=helvetica, arial, verdana,] and when I have compiled one of the samples and run it then close it I get an error message saying "Unhandled exception at 0x010a1883 in test.exe: 0xC0000005: Access violation reading location 0x00000000.". What does this mean? I have checked that I have typed out all the code correctly and copied and pasted the sample into VC++ which still gives me the error. I think the function causing the problem is:[/font]


void CleanD3D(void)
{
swapchain->Release();
dev->Release();
devcon->Release();
}



There is a yellow arrow next to it (I'm a beginner and I'm not entirely sure what it means but I'm guessing it means there is an error on that line). I assume that the codes not the problem because I got it from a tutorial so it must be a configuration problem. Can someone tell help me find what could be wrong? The rest of the code is below:

#include <windows.h>
#include <windowsx.h>
#include <d3d11.h>
#include <d3dx11.h>
#include <d3dx10.h>
#pragma comment (lib, "d3d11.lib")
#pragma comment (lib, "d3dx11.lib")
#pragma comment (lib, "d3dx10.lib")IDXGISwapChain *swapchain;
ID3D11Device *dev;
ID3D11DeviceContext *devcon;
void InitD3D(HWND hWnd);
void CleanD3D(void);LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
HWND hWnd;WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW |CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
wc.lpszClassName = "WindowClass1";
RegisterClassEx(&wc);RECT wr = {0,0,800,600};
AdjustWindowRect(&wr, WS_OVERLAPPEDWINDOW, FALSE);hWnd = CreateWindowEx(NULL, "WindowClass1", "Our first windowed program", WS_OVERLAPPEDWINDOW, 300, 100, wr.right - wr.left, wr.bottom - wr.top, NULL, NULL, hInstance, NULL);
ShowWindow(hWnd, nCmdShow);InitD3D(hWnd);
MSG msg={0};while(TRUE)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg); if(msg.message == WM_QUIT)
break;
}
else
{
}
}
CleanD3D();
return msg.wParam;
}LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wparam, LPARAM lparam)
{
switch(message)
{
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
} break;
}
return DefWindowProc(hWnd, message, wparam, lparam);
}void InitD3D(HWND hWnd)
{
DXGI_SWAP_CHAIN_DESC scd;
ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));scd.BufferCount = 1;
scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
scd.OutputWindow = hWnd;
scd.SampleDesc.Count = 4;
scd.Windowed = TRUE;
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
NULL,
NULL,
NULL,
D3D11_SDK_VERSION,
&scd,
&swapchain,
&dev,
NULL,
&devcon);
}[color=#ff0000]void CleanD3D(void)
{
swapchain->Release();
dev->Release();
devcon->Release();
}


Thanks in advance
Advertisement
One of those pointers (or all of them) is null.

Unhandled exception at 0x010a1883 in test.exe: 0xC0000005: Access violation reading location 0x00000000.

Means: At the code address of 0x010a1883 you attempted to access memory location 0x00000000. This generated an access violation (0xC0000005).

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

What's almost certainly happened is that D3D11CreateDeviceAndSwapChain() has failed. That means that dev, devcon and swapchain pointers are NULL. The tutorial code being from that site doesn't bother to check if it's failed, and the program crashes on exit trying to release them.

A simple fix for that crash is to do "if (swapchain != NULL) swapchain->Release();" and the same thing for the other three lines.

To get it functional I'd firstly suggest creating a debug device which should give you some output on the debug console telling you what's gone wrong. You could also try [font=monospace]changing the driver type to [/font]D3D_DRIVER_TYPE_WARP which uses the software renderer and should help if you have hardware or driver issues.

// This is how you create a debug device
D3D11CreateDeviceAndSwapChain(NULL,
D3D_DRIVER_TYPE_HARDWARE,
NULL,
D3D11_CREATE_DEVICE_DEBUG,
NULL,
// ...
D3D_DRIVER_TYPE_WARP does make it work so it must be a hardware problem. I re-installed the driver but it still didn't fix. Does anyone know what I could do to use the hardware? Thanks for the help.
That indicates that you're trying to do something your card doesn't support - and that particular site is nutoriously bad for checking that things succeed or are supported.

D3D_DRIVER_TYPE_WARP does make it work so it must be a hardware problem. I re-installed the driver but it still didn't fix. Does anyone know what I could do to use the hardware? Thanks for the help.


What graphics card do you have? Posting a dxdiag report would be helpful.

Also what error messages does the debug version print in the output window when you try and set up a hardware device?

This topic is closed to new replies.

Advertisement