weird exception

Started by
7 comments, last by TheAdmiral 17 years ago
hi.. recently i started learning Game Programming from the "Game Programming all in one" book. anyway, there is a part when i'm using DirectX libraries and Methods. one of the method is 'CreateDevice' which keeps giving me the next exception Unhandled exception at 0x00411844 in PracticeDirectX.exe: 0xC0000005: Access violation reading location 0x00000000. I think the problam is because the object that activate the 'CreateDevice' method valus is null. here is the whole code, maybe you can help me figure this out.

// Main.cpp

// Mirus window framework header
#include "Mirus.h"

// Direct3D header
#include <d3d9.h>


// custom derived class
class D3DWindow : public mrWindow
{
	// Direct3D interfaces
	LPDIRECT3D9			m_pD3D;
	LPDIRECT3DDEVICE9	m_pD3DDevice;

public:
	// Ctor / Dtor
	D3DWindow(){};
	~D3DWindow(){};

	// Setup and shutdown Direct3D
	HRESULT	SetupDirect3D();
	HRESULT	KillDirect3D();

	// Windows manipulation functions
	mrBool32 Frame();
};


// Initialized Direct3D
HRESULT	D3DWindow::SetupDirect3D()
{
	// Create the Direct3D object
	if (NULL ==	(m_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
	{
		return E_FAIL;
	}

	// Get the current display mode so we can know what bitdepth we are
	D3DDISPLAYMODE d3ddm;
	if (FAILED(m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
	{
		return E_FAIL;
	}

	// Fill the present parameters
	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	// We want window mode
	d3dpp.Windowed = TRUE;
	// Discard this
	d3dpp.BackBufferFormat = d3ddm.Format;

	// Create the device
	if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
									m_hWindow,
									D3DCREATE_SOFTWARE_VERTEXPROCESSING,
									&d3dpp,
									&m_pD3DDevice)))
	{
		return E_FAIL;
	}
	return D3D_OK;
}

// ShutDown Direct3D
HRESULT	D3DWindow::KillDirect3D()
{
	// If any of Direct3D objects exists, release them
	if (NULL != m_pD3D)
	{
		m_pD3D->Release();
	}

	if (NULL != m_pD3DDevice)
	{
		m_pD3DDevice->Release();
	}

	return D3D_OK;
}

// Clear the Screen to blue
mrBool32 D3DWindow::Frame()
{
	// Clear the window to blue
	m_pD3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

	// Start rending
	m_pD3DDevice->BeginScene();
	m_pD3DDevice->EndScene();

	// Present the render scence to the screen
	m_pD3DDevice->Present(NULL, NULL, NULL, NULL);
	
	return mrTrue;
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,
				   LPSTR lpCmdLine, int nShowCmd)
{
	// Our window
	D3DWindow kWindow;

	// Create a window
	kWindow.Create(hInstance, "D3D Demo");

	// Setup Direct3D
	kWindow.SetupDirect3D();

	// Enter message loop
	kWindow.Run();

	// ShutDown Direct3D
	kWindow.KillDirect3D();

	return 0;
}

Advertisement
Install the debug runtimes and see what they tell you the problem is. Assuming you have the latest SDK, go to the DirectX Control Panel under Tools in the DirectX SDK folder in the start mentu, and select "Use Debug Rumtimes" for Direct3D.

That should spit out some debug info telling you what failked and why (Looks like something or other failed). What line does the debugger break on?
Quote:Original post by Esh
Unhandled exception at 0x00411844 in PracticeDirectX.exe: 0xC0000005: Access violation reading location 0x00000000.

I think the problam is because the object that activate the 'CreateDevice' method valus is null.



It could well be that your pointer to device is null. That exception basically means you are trying to read from address 0, probably as a result of dereferencing a null pointer. Whether it's your device or not is something you should be able to find out pretty easily in your debugger.
i didn't understand how am i supposed to use this 'Debug Rumtimes'.

anyway, i think the problam is in this part of the code:

if (FAILED(m_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL,
m_hWindow,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp,
&m_pD3DDevice)))
{
return E_FAIL;
}

the 'CreateDevice' method for some reason set the m_pD3DDevice with
null(0x00000000), which cause exceptions later.

by the way, the exception is raised in any method used by the m_pD3DDevice like the 'Clear' method and not in the 'CreateDevice' Method.
Check if your m_pD3D is created normally. If it isn't then m_pD3DDevice wont be created.
Also fill d3dpp with values like:
d3dpp.Windowed = TRUE;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferWidth = 800;
d3dpp.BackBufferHeight = 600;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_ONE;
d3dpp.FullScreen_RefreshRateInHz = 60;

returning E_FAIL means that something goes wrong, you must not continue use application because your d3d device wasn't created. Exit a program.

if(FAILED(kWindow.SetupDirect3D())) return 0;
What SDK version are you using (E.g. February 2007)? The recent SDKs have an option in the start menu as I described that lets you use debug versions of D3D. Older SDKs have the DirectX control pannel in the windows control pannel. Either way, there should be an option to switch to the debug runtimes. When you're using them, you'll get a bunch of stuf spat out into your debug window whenever "exciting" things happen like devices being created or errors or warnings being generated. If any D3D call is failing, you'll get a message in your debug output telling you why it failed. An example after sabotaging my own code and setting the BackBufferCount to 100:
Quote:Direct3D9: (ERROR) :BackBufferCount must be less than D3DPRESENT_BACK_BUFFERS_MAX. ValidatePresentParameters fails.
D3D9 Helper: IDirect3D9::CreateDevice failed: D3DERR_INVALIDCALL


If CreateDevice returns a NULL device, then it must be failing, and if it fails, then your code returns E_FAIL. So you're not checking, or incorrectly interpreting the return value from the function that calls CreateDevice.
i found the problam. stupid me..
i forgot to add this line to the code
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

what probably screw things up.

thanks.
Quote:Original post by Driv3MeFar
Quote:Original post by Esh
Unhandled exception at 0x00411844 in PracticeDirectX.exe: 0xC0000005: Access violation reading location 0x00000000.

I think the problam is because the object that activate the 'CreateDevice' method valus is null.



It could well be that your pointer to device is null. That exception basically means you are trying to read from address 0, probably as a result of dereferencing a null pointer. Whether it's your device or not is something you should be able to find out pretty easily in your debugger.


Not quite. 0xC0000005 means an access violation - doing something (reading, writing, or calling a function pointer) with a pointer that is null or invalid (i.e. a pointer that's already been deleted or points to an address outside of your RAM).
hackerkey://v4sw7+8CHS$hw6+8ln6pr8O$ck4ma4+9u5Lw7VX$m0l5Ri8ONotepad++/e3+8t3b8AORTen7+9a17s0r4g8OP
Quote:Original post by nemesisgeek
Quote:Original post by Driv3MeFar
Quote:Original post by Esh
Unhandled exception at 0x00411844 in PracticeDirectX.exe: 0xC0000005: Access violation reading location 0x00000000.

I think the problam is because the object that activate the 'CreateDevice' method valus is null.



It could well be that your pointer to device is null. That exception basically means you are trying to read from address 0, probably as a result of dereferencing a null pointer. Whether it's your device or not is something you should be able to find out pretty easily in your debugger.


Not quite. 0xC0000005 means an access violation - doing something (reading, writing, or calling a function pointer) with a pointer that is null or invalid (i.e. a pointer that's already been deleted or points to an address outside of your RAM).

But the exception record says that the access violation occurs when reading location 0. That is, by definition, a null dereference.

Admiral
Ring3 Circus - Diary of a programmer, journal of a hacker.

This topic is closed to new replies.

Advertisement