Sign in to follow this  

Program crashes due to accessing a null pointer

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am using this code:
#include <windows.h>
#include <d3d9.h>
LONG FAR PASCAL WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
BOOL initD3D(HWND hwnd);
BOOL render();
BOOL release();
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
HWND hwnd;
MSG msg;
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(wc);
wc.hCursor = LoadCursor(hInstance, IDC_ARROW);
wc.hIcon = LoadIcon(hInstance, IDI_APPLICATION);
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
RegisterClassEx(&wc);

hwnd = CreateWindowEx(NULL, TEXT("Jeremy's Class"), TEXT("My Window"), WS_OVERLAPPEDWINDOW, 0, 0, 640, 480, NULL, NULL, hInstance, NULL);
initD3D(hwnd);
ShowWindow(hwnd, nShowCmd);
DWORD dw = GetTickCount();
while(TRUE)
{
if (PeekMessage(&msg, hwnd, 0, 0, 0)) 
{
	if (msg.message == WM_QUIT) 
		break;
TranslateMessage(&msg);
DispatchMessage(&msg);
}

do {
render();
} while((GetTickCount() - dw) < 25);
}
release();
return msg.wParam;
}
LONG FAR PASCAL WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_DESTROY:
	PostQuitMessage(0);
    break;
}
return DefWindowProc(hwnd,msg,wParam,lParam);
}
BOOL initD3D(HWND hwnd) 
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS pp;
ZeroMemory(&pp, sizeof(D3DPRESENT_PARAMETERS));
pp.AutoDepthStencilFormat =  D3DFMT_X8R8G8B8;
pp.BackBufferCount = 1;
pp.BackBufferFormat =  D3DFMT_X8R8G8B8;
pp.BackBufferHeight = 480;
pp.BackBufferWidth = 640;
pp.EnableAutoDepthStencil = TRUE;
pp.hDeviceWindow = hwnd;
pp.Windowed = FALSE;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
if (FAILED(d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &pp, &d3ddev)))
   return FALSE;

return TRUE;
}
BOOL render()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f,0);//This  
//is where the error keeps occurring
d3ddev->BeginScene();

d3ddev->EndScene();

d3ddev->Present(NULL, NULL, NULL, NULL);

return TRUE;
}
BOOL release()
{
d3ddev->Release();
d3d->Release();
return TRUE;
}
It is very simple and yet I am still getting this annoying run time error: Unhandled exception at 0x00411878 in d3scratch.exe: 0xC0000005: Access violation reading location 0x00000000. I have no Idea what is going on! EDIT: Please use a more appropriate subject line AND remember to use 'source' tags when posting more than a few lines of code. [Edited by - jollyjeffers on November 8, 2006 3:37:26 AM]

Share this post


Link to post
Share on other sites
You've got a null pointer access. Use your debugger, it will point at the exact location of the error.

Beside that:

A) You don't seem to check initd3d if it works. If it fails you still enter the main loop and call render accessing probably invalid pointers.

B) You're calling PeekMessage basically with PM_NOREMOVE but you do not call GetMessage afterwards. This means your message queue is not being emptied.

Share this post


Link to post
Share on other sites

This topic is 4051 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this