DirectX Access Violation Error

Im new to DirectX and all im trying to do is load a model. However when I run the app I am getting a Access Violation error

Unhandled exception at 0x00d51e85 in Project1.exe: 0xC0000005: Access violation reading location 0x00000000.

on the line containing "D3DXMATERIAL* d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();"

If anyone can point me on how to fix this and get the darn thing to work id be very appreciative.

#include <windows.h>
#include <windowsx.h>
#include <d3d9.h>
#include <d3dx9.h>
// include the Direct3D Library files
#pragma comment (lib, "d3d9.lib")
#pragma comment (lib, "d3dx9.lib")
struct MODEL
    LPD3DXMESH mesh;		//the vertices stored in a mesh
    D3DMATERIAL9* materials;	//materials applied to polygons
    LPDIRECT3DTEXTURE9* textures;	//textures applied to polygons
    DWORD material_count;		//number of materials in mesh
// global declarations
LPDIRECT3D9 d3d;    // the pointer to our Direct3D interface
LPDIRECT3DDEVICE9 d3ddev;    // the pointer to the device class
// mesh declarations
LPD3DXMESH mesh;    // define the mesh pointer
//define the material buffer
LPD3DXBUFFER matbuffer;
D3DXMATERIAL* d3dxMaterials;
//allocate the mesh buffer in memory
MODEL *model = (MODEL*)malloc(sizeof(MODEL));
#define KEY_DOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEY_UP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
// function prototypes
void initD3D(HWND hWnd);    // sets up and initializes Direct3D
void render_frame(void);    // renders a single frame
void cleanD3D(void);    // closes Direct3D and releases memory
void init_graphics(void);    // 3D declarations
void init_light(void);    // sets up the light and the material
// the WindowProc function prototype
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
    HWND hWnd;
    ZeroMemory(&wc, sizeof(WNDCLASSEX));
    wc.cbSize = sizeof(WNDCLASSEX); = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "Project 2b";
    hWnd = CreateWindowEx(NULL, "Project 2a", "Phil Loyer - Project 2b",
                          WS_OVERLAPPEDWINDOW, 300, 300, 1024, 768,
                          NULL, NULL, hInstance, NULL);
    ShowWindow(hWnd, nCmdShow);
    // set up and initialize Direct3D
    // enter the main loop:
    MSG msg;
        DWORD starting_point = GetTickCount();
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            if (msg.message == WM_QUIT)
        // check the 'escape' key
            PostMessage(hWnd, WM_DESTROY, 0, 0);
        while ((GetTickCount() - starting_point) < 25);
    // clean up DirectX and COM
    return msg.wParam;
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
        case WM_DESTROY:
                return 0;
            } break;
    return DefWindowProc (hWnd, message, wParam, lParam);
// this function initializes and prepares Direct3D for use
void initD3D(HWND hWnd)
    d3d = Direct3DCreate9(D3D_SDK_VERSION);
    ZeroMemory(&d3dpp, sizeof(d3dpp));
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.hDeviceWindow = hWnd;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
    // create a device class using this information and the info from the d3dpp stuct
    d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);  
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    
    d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    
// this is the function used to render a single frame
void render_frame(void)
      // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    &D3DXVECTOR3 (0.0f, 3.0f, 6.0f),    // the camera position
    &D3DXVECTOR3 (0.0f, 0.0f, 0.0f),      // the look-at position
    &D3DXVECTOR3 (0.0f, 1.0f, 0.0f));    // the up direction
    d3ddev->SetTransform(D3DTS_VIEW, &matView);    // set the view transform to matView 
      // set the projection transform
    D3DXMATRIX matProjection;    // the projection transform matrix
                               D3DXToRadian(45),    // the horizontal field of view
                               (FLOAT)1024 / (FLOAT)768, // aspect ratio
                               1.0f,    // the near view-plane
                               100.0f);    // the far view-plane
    d3ddev->SetTransform(D3DTS_PROJECTION, &matProjection);    // set the projection
	int numOfMaterials = model->material_count;
	for(int i=0; i < numOfMaterials; i++)
		d3ddev->SetTexture(0, model->textures[i]);
    d3ddev->Present(NULL, NULL, NULL, NULL);
// this is the function that cleans up Direct3D and COM
void cleanD3D(void)
    mesh->Release();    // close and release the sphere mesh
    d3ddev->Release();    // close and release the 3D device
    d3d->Release();    // close and release Direct3D
// this is the function that puts the 3D models into video RAM
void init_graphics(void)
	HRESULT result;
	//load the mesh file
	result = D3DXLoadMeshFromX(
    "motorbike1.x",               //filename
    D3DXMESH_SYSTEMMEM,     //mesh options
    d3ddev,                 //Direct3D device
    NULL,                   //adjacency buffer
    &matbuffer,             //material buffer
    NULL,                   //special effects
    &model->material_count, //number of materials
    &model->mesh);          //resulting mesh
	D3DXMATERIAL* d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();
	model->materials = new D3DMATERIAL9[model->material_count];
	model->textures  = new LPDIRECT3DTEXTURE9[model->material_count];
	int numOfMaterials = model->material_count;
	for(int i=0; i < numOfMaterials; i++)
		//grab the material
		model->materials[i] = d3dxMaterials[i].MatD3D;
		//set ambient color for material 
		model->materials[i].Ambient = model->materials[i].Diffuse;
		model->textures[i] = NULL;
		if( d3dxMaterials[i].pTextureFilename != NULL && 
			lstrlen(d3dxMaterials[i].pTextureFilename) > 0 )
			//load texture file specified in .x file
			result = D3DXCreateTextureFromFile(d3ddev, 
// this is the function that sets up the lights and materials
void init_light(void)
    D3DLIGHT9 light;    // create the light struct
    D3DMATERIAL9 material;    // create the material struct
    ZeroMemory(&light, sizeof(light));    // clear out the struct for use
    light.Type = D3DLIGHT_DIRECTIONAL;    // make the light type 'directional light'
    light.Diffuse.r = 0.5f;    // .5 red
    light.Diffuse.g = 0.5f;    // .5 green
    light.Diffuse.b = 0.5f;    // .5 blue
    light.Diffuse.a = 1.0f;    // full alpha (we'll get to that soon)
    D3DVECTOR vecDirection = {-1.0f, -0.3f, -1.0f};    // the direction of the light
    light.Direction = vecDirection;    // set the direction
    d3ddev->SetLight(0, &light);    // send the light struct properties to light #0
    d3ddev->LightEnable(0, TRUE);    // turn on light #0
    ZeroMemory(&material, sizeof(D3DMATERIAL9));    // clear out the struct for use
    material.Diffuse.r = material.Ambient.r = 1.0f;    // set the material to full red
    material.Diffuse.g = material.Ambient.g = 1.0f;    // set the material to full green
    material.Diffuse.b = material.Ambient.b = 1.0f;    // set the material to full blue
    material.Diffuse.a = material.Ambient.a = 1.0f;    // set the material to full alpha
    d3ddev->SetMaterial(&material);    // set the globably-used material to &material

Open in new window

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You need to check the return value of 'D3DXLoadMeshFromX()' (, since this call probably failed and set 'matbuffer' to NULL.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PoolcorpAuthor Commented:
You were right about the Load failing, any ideas as to maybe why?
The values going into the method call are:

d3ddev = 0x00000000
matbuffer = 0x00000000
model->material_count = 3452816845
model->mesh = 0xcdcdcdcd

and the return result is -2005530516

I search google for that but nothing came back.

Thanks for your help
the return value is


Per the docs, that's INVALID CALL....

Do all of the parameters passed to the call look right to you?


d3ddev = 0x0000000
matbuffer = 0x00000000

Per the docs:
[in] Pointer to a string that specifies the filename. If the compiler settings require Unicode, the data type LPCTSTR resolves to LPCWSTR. Otherwise, the string data type resolves to LPCSTR. See Remarks.
[in] Combination of one or more flags from the D3DXMESH enumeration, which specifies creation options for the mesh.
[in] Pointer to an IDirect3DDevice9 interface, the device object associated with the mesh.
[out] Pointer to a buffer that contains adjacency data. The adjacency data contains an array of three DWORDs per face that specify the three neighbors for each face in the mesh. For more information about accessing the buffer, see ID3DXBuffer.
[out] Pointer to a buffer containing materials data. The buffer contains an array of D3DXMATERIAL structures, containing information from the DirectX file. For more information about accessing the buffer, see ID3DXBuffer.
[out] Pointer to a buffer containing an array of effect instances, one per attribute group in the returned mesh. An effect instance is a particular instance of state information used to initialize an effect. See D3DXEFFECTINSTANCE. For more information about accessing the buffer, see ID3DXBuffer.
[out] Pointer to the number of D3DXMATERIAL structures in the ppMaterials array, when the method returns.
[out] Address of a pointer to an ID3DXMesh interface, representing the loaded mesh.

matbuffer is the output buffer... 0x0000000 is null.

you need to allocate the buffer.

and d3ddev is null, too.  You need to fix that.


CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

d3ddev must be a valid pointer to a d3d device object.
make sure the call to init_graphics is after initD3D and that initD3D succeeded.

mabuffer is a pointer, initialized to be NULL which is fine, the helper function needs the address of this pointer to set it on a successful load.

I also checked the function help, I think those parameters you pass as NULL are not optional, so you need to pass a valid address just like matbuffer, so your function needs to be like:

ID3DXBuffer * pTempAdj = NULL;
ID3DXBuffer * pTempSFX = NULL;

//load the mesh file
result = D3DXLoadMeshFromX(
    "motorbike1.x",               //filename
    D3DXMESH_SYSTEMMEM,     //mesh options
    d3ddev,                 //Direct3D device
    &pTempAdj,                   //adjacency buffer
    &matbuffer,             //material buffer
    &pTempSFX,                   //special effects
    &model->material_count, //number of materials
    &model->mesh);          //resulting mesh
if( pTempAdj ) pTempAdj->Release();
if( pTempSFX ) pTempSFX->Release();
oh and one more this,
you may want to initialize the mesh data,

model->material_count = 0;
model->mesh = 0;
>>abuffer is a pointer, initialized to be NULL which is fine, the helper function needs the address of this pointer to >>set it on a successful load.


But the variable you pass in CAN'T be null.

the contents of the variable is null. Not the address of where to put the buffer.

And he's passing in a null.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Game Programming

From novice to tech pro — start learning today.