?
Solved

DirectX Access Violation Error

Posted on 2008-11-09
8
Medium Priority
?
1,696 Views
Last Modified: 2013-12-21
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;
    WNDCLASSEX wc;
 
    ZeroMemory(&wc, sizeof(WNDCLASSEX));
 
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = (WNDPROC)WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.lpszClassName = "Project 2b";
 
    RegisterClassEx(&wc);
 
    hWnd = CreateWindowEx(NULL, "Project 2a", "Phil Loyer - Project 2b",
                          WS_OVERLAPPEDWINDOW, 300, 300, 1024, 768,
                          NULL, NULL, hInstance, NULL);
 
	init_graphics();  
 
    ShowWindow(hWnd, nCmdShow);
 
    // set up and initialize Direct3D
    initD3D(hWnd);
 
    // enter the main loop:
 
    MSG msg;
 
    while(TRUE)
    {
        DWORD starting_point = GetTickCount();
 
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            if (msg.message == WM_QUIT)
                break;
 
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
 
        render_frame();
 
        // check the 'escape' key
        if(KEY_DOWN(VK_ESCAPE))
            PostMessage(hWnd, WM_DESTROY, 0, 0);
 
        while ((GetTickCount() - starting_point) < 25);
    }
 
    // clean up DirectX and COM
    cleanD3D();
 
    return msg.wParam;
}
 
 
// this is the main message handler for the program
LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch(message)
    {
        case WM_DESTROY:
            {
                PostQuitMessage(0);
                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);
 
    D3DPRESENT_PARAMETERS d3dpp;
 
    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
    d3d->CreateDevice(D3DADAPTER_DEFAULT,
                      D3DDEVTYPE_HAL,
                      hWnd,
                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                      &d3dpp,
                      &d3ddev);
 
  
    init_light();   
 
    d3ddev->SetRenderState(D3DRS_LIGHTING, TRUE);  
    d3ddev->SetRenderState(D3DRS_ZENABLE, TRUE);    
    d3ddev->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(50, 50, 50));    
 
    return;
}
 
 
// this is the function used to render a single frame
void render_frame(void)
{
 
    d3ddev->BeginScene();
 
      // set the view transform
    D3DXMATRIX matView;    // the view transform matrix
    D3DXMatrixLookAtLH(&matView,
    &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
    D3DXMatrixPerspectiveFovLH(&matProjection,
                               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->SetMaterial(&model->materials[i]);
		d3ddev->SetTexture(0, model->textures[i]);
		model->mesh->DrawSubset(i);
	}
	
 
    d3ddev->EndScene(); 
 
    d3ddev->Present(NULL, NULL, NULL, NULL);
 
    return;
}
 
 
// 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
 
    return;
}
 
 
 
// 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, 
				d3dxMaterials[i].pTextureFilename, 
				&model->textures[i]);
		}
	}
 
 
 
    return;
}
 
 
// 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
 
    return;
}

Open in new window

0
Comment
Question by:Poolcorp
6 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 22917114
You need to check the return value of 'D3DXLoadMeshFromX()' (http://msdn.microsoft.com/en-us/library/bb172890.aspx), since this call probably failed and set 'matbuffer' to NULL.
0
 
LVL 3

Author Comment

by:Poolcorp
ID: 22918355
You were right about the Load failing, any ideas as to maybe why?
The values going into the method call are:

D3DXMESH_SYSTEMMEM = 272,
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
0
 
LVL 11

Assisted Solution

by:jgordos
jgordos earned 1000 total points
ID: 22919394
the return value is

0x8876086C

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

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

ie,

d3ddev = 0x0000000
matbuffer = 0x00000000

Per the docs:
pFilename
[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.
Options
[in] Combination of one or more flags from the D3DXMESH enumeration, which specifies creation options for the mesh.
pD3DDevice
[in] Pointer to an IDirect3DDevice9 interface, the device object associated with the mesh.
ppAdjacency
[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.
ppMaterials
[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.
ppEffectInstances
[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.
pNumMaterials
[out] Pointer to the number of D3DXMATERIAL structures in the ppMaterials array, when the method returns.
ppMesh
[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.

-john

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:romanm
ID: 22919502
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();
 
0
 
LVL 3

Expert Comment

by:romanm
ID: 22919510
oh and one more this,
you may want to initialize the mesh data,

model->material_count = 0;
model->mesh = 0;
0
 
LVL 11

Expert Comment

by:jgordos
ID: 22925867
>>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.

Right.

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.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question