12 Replies - 3688 Views - Last Post: 06 March 2014 - 05:38 PM Rate Topic: -----

#1 Ambitious   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Access Violation to Vertexbuffer

Posted 05 March 2014 - 08:26 PM

I keep getting this:
Posted Image
Can you guys help me? I'm not understanding whats wrong here.
#include <Windows.h>
#include <d3d9.h>
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE)

LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
HINSTANCE Inst;
HWND Handle;
VOID* pVoid;
LPDIRECT3DVERTEXBUFFER9 v_buffer=NULL;
void cleanUp();
void initgfx(void);
void initD3D(HWND hWnd);
void render_frame(void);
bool initWindow(HINSTANCE Hinst);
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
struct CUSTOMVERTEX
{
	FLOAT x,y,z,rhw;
	DWORD color;
};

int WINAPI WinMain(HINSTANCE Inst,HINSTANCE PrevInst,LPSTR CmdLine,int CmdShow)
{
	if(!initWindow(Inst))
		return false;
	initD3D(Handle);

	MSG msg;
	while(true)
	{
		while(PeekMessage(&msg,Handle,0,0,PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		if(msg.message==WM_QUIT)
		{
			break;
		}
		render_frame();
	}
	cleanUp();
}

bool initWindow(HINSTANCE Hinst)
{
	WNDCLASSEX wcx;
	ZeroMemory(&wcx,sizeof(wcx));
	wcx.cbClsExtra=0;
	wcx.cbSize=sizeof(WNDCLASSEX);
	wcx.hInstance=Hinst;
	wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
	wcx.hIcon=0;
	wcx.hIconSm=0;
	wcx.hbrBackground=(HBRUSH)(COLOR_BACKGROUND+1);
	wcx.lpfnWndProc=WndProc;
	wcx.lpszClassName=L"Onmyown";
	wcx.lpszMenuName=0;
	wcx.style=CS_VREDRAW | CS_HREDRAW;
	RegisterClassEx(&wcx);
	Handle=CreateWindowA("Onmyown","DirectX alone",WS_OVERLAPPEDWINDOW,0,0,340,220,NULL,NULL,Hinst,NULL);
	ShowWindow(Handle,1);
	UpdateWindow(Handle);
	return true;
}

LRESULT CALLBACK WndProc(HWND Hand,UINT Msg,WPARAM Wp,LPARAM Lp)
{
	switch(Msg)
	{
	case WM_DESTROY:
		{
		    PostQuitMessage(1);
	       	return 0;
		}
		break;

	}
	return DefWindowProc(Hand,Msg,Wp,Lp);
}

void initD3D(HWND hWnd)
{
	d3d=Direct3DCreate9(D3D_SDK_VERSION);
	D3DPRESENT_PARAMETERS d3dp;
	d3dp.Windowed=TRUE;
	d3dp.SwapEffect=D3DSWAPEFFECT_DISCARD;
	d3dp.hDeviceWindow=hWnd;
	d3dp.BackBufferFormat=D3DFMT_X8R8G8B8;
	d3dp.BackBufferWidth=340;
	d3dp.BackBufferHeight=220;
	d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dp,&d3ddev);
	initgfx();
}
void render_frame(void)
{
	d3ddev->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0f,0);
	d3ddev->BeginScene();
	d3ddev->SetFVF(CUSTOMFVF);
	//Select vertexbuffer to display
	d3ddev->SetStreamSource(0,v_buffer,0,sizeof(CUSTOMVERTEX));
	//Copy vertexbuffer to backbuffer
	d3ddev->DrawPrimitive(D3DPT_TRIANGLELIST,0,1);
	d3ddev->EndScene();
	d3ddev->Present(NULL,NULL,NULL,NULL);
}

void cleanUp()
{
	v_buffer->Release();
	d3ddev->Release();
	d3d->Release();
}
void initgfx(void)
{
	CUSTOMVERTEX vertices[]=
	{
		{400.0f,62.5f,0.5f,1.0f,D3DCOLOR_XRGB(0,0,225),},
		{650.0,500.0f,0.5f,1.0f,D3DCOLOR_XRGB(0,225,0),},
		{150.0,500.0f,0.5f,1.0f,D3DCOLOR_XRGB(225,0,0),},
	};
	d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&v_buffer,NULL);
	VOID* pVoid;
	v_buffer->Lock(0,0,(void**)&pVoid,0);
	memcpy(pVoid,vertices,sizeof(vertices));
	v_buffer->Unlock();
}


:code:

This post has been edited by GunnerInc: 05 March 2014 - 08:29 PM
Reason for edit:: Code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Access Violation to Vertexbuffer

#2 GunnerInc   User is offline

  • "Hurry up and wait"
  • member icon




Reputation: 918
  • View blog
  • Posts: 2,358
  • Joined: 28-March 11

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 08:29 PM

Moving out of "Advanced Discussion" and fixing those botched code tags...
Was This Post Helpful? 0
  • +
  • -

#3 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 08:43 PM

maybe do you not have the lib linked?
did you enter the arguments wrong?
check the hresult returned might give you a better idea what error it causes
Was This Post Helpful? 0
  • +
  • -

#4 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 08:57 PM

The message box you receive tell's you that you are trying to dereference a NULL pointer (access violation read at 0x00).

CreateVertexBuffer expects a type of IDirect3DVertexBuffer9 **, however you are only passing a IDirect3DVertexBuffer9 *.

This post has been edited by jjl: 05 March 2014 - 09:00 PM

Was This Post Helpful? -1
  • +
  • -

#5 Ambitious   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 09:07 PM

Alright thanks guy I'll continue trying to solve this error. This is confusing at the moment.
Was This Post Helpful? 0
  • +
  • -

#6 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 09:15 PM

umm jjl that is why he is referencing **pp = &(*p) that the output of the function and should change what v_buffer points to
Was This Post Helpful? 0
  • +
  • -

#7 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 09:35 PM

The access violation is from failure to create the device check all of your initD3D() function.
Also check the hresult with failed() you can use OutputDebugString(L"string") for debugging spots.
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2252
  • View blog
  • Posts: 4,343
  • Joined: 30-May 10

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 11:06 PM

There's a little yellow arrow pointing at this line.
> 123 d3ddev->CreateVertexBuffer(3*sizeof(CUSTOMVERTEX),0,CUSTOMFVF,D3DPOOL_MANAGED,&v_buffer,NULL);

In the "autos" box at the bottom left, it's showing you the current value of d3ddev, which is NULL.

> 093 d3d->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dp,&d3ddev);
You should put a breakpoint on this line, and find out why d3ddev isn't being assigned a value.
Was This Post Helpful? 1
  • +
  • -

#9 jjl   User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: Access Violation to Vertexbuffer

Posted 05 March 2014 - 11:29 PM

@infernorthor

I missed the LP prefix on LPIDirect3DVertexBuffer9, thus making &v_buffer a address of a pointer.
Was This Post Helpful? 0
  • +
  • -

#10 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Access Violation to Vertexbuffer

Posted 06 March 2014 - 03:32 PM

@jjl it happens

I just noticed what is wrong you didn't complete the D3DPRESENT_PARAMETERS struct always initialize to zero when you don't.
d3dp = {0};
Was This Post Helpful? 0
  • +
  • -

#11 Ambitious   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Access Violation to Vertexbuffer

Posted 06 March 2014 - 05:20 PM

View Postinfernorthor, on 06 March 2014 - 03:32 PM, said:

@jjl it happens

I just noticed what is wrong you didn't complete the D3DPRESENT_PARAMETERS struct always initialize to zero when you don't.
d3dp = {0};

I did initialize most structure values that are needed for this program.
I'm not sure whats wrong.
Was This Post Helpful? 0
  • +
  • -

#12 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: Access Violation to Vertexbuffer

Posted 06 March 2014 - 05:26 PM

Only filled out 6 of the struct. Just initialized it to {0} at line 86. Zero usually has it do the default, otherwise if it get a wrong number it won't work.

http://msdn.microsof...8(v=vs.85).aspx
Was This Post Helpful? 1
  • +
  • -

#13 Ambitious   User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 133
  • Joined: 08-May 13

Re: Access Violation to Vertexbuffer

Posted 06 March 2014 - 05:38 PM

I actually found another solution, but thanks you all helped! Especially infernorthor.
I zeroed the memory block as soon as I created my D3D Parameters structure, using ZeroMemory()
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1