Sign in to follow this  

Unhandled exception error

This topic is 2586 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

hey guys
i am getting this weird error in my code, please check it out
the error is:
"Unhandled exception at 0x013f1d66 in Cube2.0.exe: 0xC0000005: access violation reading location 0xfeeeffba"

the error pops whenever i invoke SetLight() method in Init_Scene() function

the code:

#if defined(DEBUG) | defined(_DEBUG)
#ifndef D3D_DEBUG_INFO
#define D3D_DEBUG_INFO
#endif
#endif



#include<Windows.h>
#include<string.h>
#include<d3d9.h>
#include<d3dx9.h>
#include<DxErr.h>
#include<stdio.h>

#pragma comment(lib, "d3d9.lib")
#ifdef _DEBUG
#pragma comment(lib, "d3dx9d.lib")
#else
#pragma comment(lib, "d3dx9.lib")
#endif
#pragma comment(lib, "dxerr.lib")


#if defined(DEBUG) | defined(_DEBUG)
#ifndef HR
#define HR(x) { HRESULT hr = x; if(FAILED(hr)) { DXTrace(__FILE__, __LINE__, hr, #x, TRUE); } }
#endif

#else
#ifndef HR
#define HR(x) x;
#endif
#endif



typedef struct Vertex{
float x, y, z;
float nx, ny, nz;
D3DCOLOR Diffuse;
}Vertex;

Vertex gCube[] = {
//Front Face
{ -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255)},
{ 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255, 255, 255, 255)},
{ 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255,255,255,255)},
{-1.0f, -1.0f, -1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(255,255,255,255)},


//Back Face
{ -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 255, 255)},
{ 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(0, 0, 255, 255)},
{ 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(0,0,255,255)},
{-1.0f, -1.0f, 1.0f, 0.0f, 0.0f, -1.0f, D3DCOLOR_RGBA(5,0,255,255)},

//Right Face
{ 1.0f, 1.0f, -1.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255)},
{ 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255, 0, 0, 255)},
{ 1.0f, -1.0f, 1.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255,0,0,255)},
{ 1.0f, -1.0f, -1.0f, 1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(255,0,0,255)},

//Left Face
{ -1.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(0, 255, 0, 255)},
{ -1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(0, 255, 0, 255)},
{ -1.0f, -1.0f, 1.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(0,255,0,255)},
{ -1.0f, -1.0f, -1.0f, -1.0f, 0.0f, 0.0f, D3DCOLOR_RGBA(0,255,0,255)},

//Top Face
{ 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(205, 205, 205, 155)},
{ 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(205, 205, 205, 155)},
{ -1.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(205,205,205,155)},
{-1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, D3DCOLOR_RGBA(245,245,245,155)},

//Buttom Face
{ -1.0f, -1.0f, -1.0f, 0.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(155, 255, 255, 255)},
{ -1.0f, -1.0f, 1.0f, 0.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(155, 255, 255, 255)},
{ 1.0f, 1.0f, 1.0f, 0.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(155,255,255,255)},
{ 1.0f, 1.0f, -1.0f, 0.0f, -1.0f, 0.0f, D3DCOLOR_RGBA(155,255,255,255)},

};

#define ReleaseCOM(x) if(x) x->Release()

#define gCubeFVF (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_DIFFUSE)

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define NUMOFVERTEX sizeof(gCube)/sizeof(Vertex)

LPDIRECT3D9 gd3d;
LPDIRECT3DDEVICE9 gd3dDevice;
LPDIRECT3DVERTEXBUFFER9 gVB;
LPDIRECT3DVERTEXSHADER9 gVS;
bool gFullScreen;
bool gAppDone = FALSE;
HWND gHwnd = NULL;
HINSTANCE gHinstance = NULL;
const char gAppName[] = "Cube";
float gXspeed;
float gYspeed;
float gZpos;
int gFilter;
bool gLighting = false;


bool DoFullScreen();
void Init_Window();
D3DFORMAT GetFormat();
void Init_Device();
void Init_Scene();
void Run();
void Render();
void Kill_Window();
void Kill_Device();
void Kill_Scene();
void Error(const char* Message);
void GetError();
LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wparam, LPARAM lparam);
int APIENTRY WinMain(HINSTANCE Hins, HINSTANCE pHins, LPSTR CmdLine, int CmdShow);

int APIENTRY WinMain(HINSTANCE Hins, HINSTANCE pHins, LPSTR CmdLine, int CmdShow)
{
gHinstance = Hins;
Init_Window();
Init_Device();
Init_Scene();

Run();

Kill_Scene();
Kill_Device();
Kill_Window();

return 0;
}

bool DoFullScreen()
{
HRESULT hr;
hr = MessageBox(NULL, "Full Screen???", gAppName, MB_YESNO|MB_ICONQUESTION);
switch(hr)
{
case IDYES:
gFullScreen = true;
return true;
break;
case IDNO:
gFullScreen = false;
return false;
break;
case IDCANCEL:
MessageBox(NULL, "App Exiting", gAppName, MB_OK);
exit(0);
break;
}
}

void Init_Window()
{
WNDCLASS Wclass;
Wclass.hInstance = gHinstance;
Wclass.cbClsExtra = 0;
Wclass.cbWndExtra = 0;
Wclass.lpszClassName = "WC";
Wclass.lpszMenuName = NULL;
Wclass.lpfnWndProc = WinProc;
Wclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
Wclass.hCursor = LoadIcon(NULL, IDC_ARROW);
Wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
Wclass.style = CS_OWNDC;


if(!RegisterClass(&Wclass))
{
GetError();
Error("Unable to Register Class");
}

ULONG Window_Width;
ULONG Window_Height;
DWORD style;

if(DoFullScreen())
{
Window_Width = GetSystemMetrics(SM_CXSCREEN);
Window_Height = GetSystemMetrics(SM_CYSCREEN);
style = WS_POPUP;
}
else
{
Window_Width = SCREEN_WIDTH;
Window_Height = SCREEN_HEIGHT;
style = WS_OVERLAPPED|WS_SYSMENU;
}

gHwnd = CreateWindow("WC"
, gAppName , style , 0
, 0
, Window_Width
, Window_Height
, GetDesktopWindow()
, NULL
, gHinstance
, NULL);

if(!gHwnd){
GetError();
Error("Unable to create the main window");
}

ShowWindow(gHwnd, SW_SHOW);
UpdateWindow(gHwnd);
SetFocus(gHwnd);

}

D3DFORMAT GetFormat()
{
if(SUCCEEDED(gd3d->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_R5G6B5, D3DFMT_R5G6B5, FALSE)))
return D3DFMT_R5G6B5;
else if(SUCCEEDED(gd3d->CheckDeviceType(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, D3DFMT_X1R5G5B5, D3DFMT_X1R5G5B5, FALSE)))
return D3DFMT_X1R5G5B5;
else
Error("Unable to get 16-bit display format");

return (D3DFORMAT)NULL;

}
void Init_Device()
{
HRESULT hr;
D3DPRESENT_PARAMETERS d3dpp;
D3DDISPLAYMODE d3ddm;

memset(&d3dpp, 0, sizeof(d3dpp));

gd3d = Direct3DCreate9(D3D_SDK_VERSION);

hr = gd3d->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm);
if(FAILED(hr))
Error("Unable to get display mode");

if(gFullScreen)
{
d3dpp.Windowed = false;
d3dpp.BackBufferHeight = SCREEN_HEIGHT;
d3dpp.BackBufferWidth = SCREEN_WIDTH;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = GetFormat();
d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
}
else
{
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
d3dpp.Windowed = true;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.FullScreen_RefreshRateInHz = 0;
}

d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D24S8;
d3dpp.EnableAutoDepthStencil = true;
d3dpp.BackBufferCount = 1;
d3dpp.hDeviceWindow = gHwnd;
d3dpp.MultiSampleQuality = 0;
d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE;

HRESULT Hr;
Hr = gd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, gHwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING , &d3dpp, &gd3dDevice);

if(FAILED(Hr))
{
GetError();
Error("Unable to create D3D device");
}

ReleaseCOM(gd3d);
ReleaseCOM(gd3dDevice);
}

void Init_Scene()
{
D3DLIGHT9 d3dLight;
D3DXMATRIX MatView;
D3DXMATRIX MatPers;

ZeroMemory(&d3dLight, sizeof(d3dLight));
d3dLight.Type = D3DLIGHT_POINT;
d3dLight.Range = 100.0f;
d3dLight.Position = D3DXVECTOR3(0.0f, 1.0f, -5.0f);
d3dLight.Diffuse.r = 1.0f;
d3dLight.Diffuse.g = 1.0f;
d3dLight.Diffuse.b = 1.0f;
d3dLight.Ambient = d3dLight.Diffuse;

if(gd3dDevice)
if(FAILED(gd3dDevice->SetLight(0,&d3dLight)))
GetError();

gd3dDevice->LightEnable(0, TRUE);

gd3dDevice->SetRenderState(D3DRS_LIGHTING, gLighting);
gd3dDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
gd3dDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1);
gd3dDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE);
gd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
gd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
gd3dDevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_RGBA(255, 255, 255, 255));
gd3dDevice->SetRenderState(D3DRS_ZENABLE, TRUE);

D3DXVECTOR3 Eye;
D3DXVECTOR3 Up;
D3DXVECTOR3 At;

Eye = D3DXVECTOR3( 0.0f, 0.0f, -10.0f);
Up = D3DXVECTOR3( 0.0f, 1.0f, 0.0f);
At = D3DXVECTOR3( 0.0f, 0.0f, 0.0f);

D3DXMatrixLookAtLH(&MatView, &Eye, &At, &Up);

gd3dDevice->SetTransform(D3DTS_VIEW, &MatView);

D3DXMatrixPerspectiveFovLH(&MatPers, D3DX_PI/4, ((float)SCREEN_WIDTH / (float)SCREEN_HEIGHT), 1.0f, 1000.0f);

gd3dDevice->SetTransform(D3DTS_PROJECTION, &MatPers);


HRESULT hr;
unsigned char* Data;
hr = gd3dDevice->CreateVertexBuffer( (NUMOFVERTEX) * (sizeof(Vertex)), NULL, gCubeFVF, D3DPOOL_MANAGED, &gVB, NULL);

if(FAILED(hr))
Error("Unable to Create Vertex Buffer");

hr = gVB->Lock(0, 0, (void**)&Data, 0);
if(FAILED(hr))
Error("Unable to lock vertex buffer");

memcpy(Data, gCube, sizeof(Vertex));
gVB->Unlock();



}

void Render()
{
D3DXMATRIX MatWorld;
D3DXMATRIX MatWorld_X;
D3DXMATRIX MatWorld_Y;
D3DXMATRIX MatWorld_TRANS;
float xRot = 0;
float yRot = 0;
float zPos = 5;

xRot += gXspeed;
yRot += gYspeed;
zPos += gZpos;

gd3dDevice->Clear(1, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_RGBA(255, 255, 255, 255), 1.0f, 1.0f);

gd3dDevice->BeginScene();

gd3dDevice->SetStreamSource(0, gVB, 0, 0);
gd3dDevice->SetVertexShader(gVS);

D3DXMatrixIdentity(&MatWorld);
D3DXMatrixIdentity(&MatWorld_X);
D3DXMatrixIdentity(&MatWorld_Y);
D3DXMatrixIdentity(&MatWorld_TRANS);
D3DXMatrixRotationX(&MatWorld_X, xRot);
D3DXMatrixRotationY(&MatWorld_Y, yRot);
D3DXMatrixMultiply(&MatWorld, &MatWorld_X, &MatWorld_Y);
D3DXMatrixTranslation(&MatWorld_TRANS, 0.0f, 0.0f, zPos);
D3DXMatrixMultiply(&MatWorld, &MatWorld, &MatWorld_TRANS);

gd3dDevice->SetTransform(D3DTS_WORLD, &MatWorld);

gd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

gd3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, sizeof(gCube)/3);

gd3dDevice->EndScene();

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

}


void Run()
{
MSG msg;

while(!gAppDone)
{
if(PeekMessage(&msg, gHwnd, NULL, NULL, PM_REMOVE))
{
if(msg.message == WM_QUIT)
{
PostQuitMessage(0);
MessageBox(NULL, "Cya!", gAppName, MB_OK);
exit(0);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
Render();
}


}

void Error(const char* Message)
{
MessageBox(NULL, Message, gAppName, MB_OKCANCEL|MB_ICONERROR);
exit(4);
}

void GetError()
{
char buffer[256];
sprintf(buffer, "GetLastError() = %lu \n", GetLastError());

MessageBox(NULL, buffer, gAppName, MB_OKCANCEL|MB_ICONERROR);
exit(4);
}


void Kill_Window()
{
if(!DestroyWindow(gHwnd))
MessageBox(gHwnd, "Cant destroy class", gAppName, MB_OK);
else
{
MSG msg;
while(PeekMessage(&msg, gHwnd, NULL, NULL, PM_REMOVE))
DispatchMessage(&msg);
}

UnregisterClass("WC", gHinstance);

gHwnd = NULL;
}

void Kill_Device()
{
if(gd3d)
{
gd3d->Release();
gd3d = NULL;
}
if(gd3dDevice)
{
gd3dDevice->Release();
gd3dDevice = NULL;
}
}

void Kill_Scene()
{
gd3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);
}

LRESULT CALLBACK WinProc(HWND hwnd, UINT Msg, WPARAM wparam, LPARAM lparam)
{
HDC hdc;
PAINTSTRUCT ps;
int Keys = (int) wparam;
switch(Msg)
{
case WM_KEYDOWN:
switch(Keys)
{
case VK_UP:
gYspeed += 0.5f;
break;
case VK_DOWN:
gYspeed -= 0.5f;
break;
case VK_RIGHT:
gXspeed += 0.5f;
break;
case VK_LEFT:
gXspeed -= 0.5f;
break;
case 'E':
gZpos += 0.5f;
break;
case 'D':
gZpos -= 0.5f;
break;
case 'L':
gLighting = !gLighting;
case VK_ESCAPE:
gAppDone = true;
break;
}
return 0;


case WM_PAINT:
hdc = BeginPaint(hwnd, &ps);
EndPaint(hwnd, &ps);
return 0;

case WM_CLOSE:
gAppDone = true;
return 0;
case WM_DESTROY:
PostQuitMessage(5);
exit(5);
return 0;
}

return (DefWindowProc(hwnd, Msg, wparam, lparam));
}




Appreciate the help

Share this post


Link to post
Share on other sites


HRESULT Hr;
Hr = gd3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, gHwnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING , &d3dpp, &gd3dDevice);

if(FAILED(Hr))
{
GetError();
Error("Unable to create D3D device");
}

ReleaseCOM(gd3d);
ReleaseCOM(gd3dDevice);



Just a quick look, but I noticed that you seem to release your device just after its creation.

Cheers!

Share this post


Link to post
Share on other sites
Quote:
Original post by VISQI
hey guys
i am getting this weird error in my code, please check it out
the error is:
"Unhandled exception at 0x013f1d66 in Cube2.0.exe: 0xC0000005: access violation reading location 0xfeeeffba"

the error pops whenever i invoke SetLight() method in Init_Scene() function
Your device has been Release()d.

Share this post


Link to post
Share on other sites

This topic is 2586 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