Sign in to follow this  
Enerjak

How to handle Access violations.

Recommended Posts

I seem to be getting alot of access violations when i program. I don't know why. like the code compiles fine but when it's ran, it like gets access violation like before i was trying to make a class to load a model and position in in the world but i got access violations. So what should I keep track ofr when dealing with Access Violations? like what does it mean when it says "Expression cannoit be evaluated" I know what it means in ENGLISH but other wise it just makes no sense since i see that my code is fine and that it compiles fine. Any thoughts?

Share this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
I seem to be getting alot of access violations when i program. I don't know why. like the code compiles fine but when it's ran, it like gets access violation like before i was trying to make a class to load a model and position in in the world but i got access violations. So what should I keep track ofr when dealing with Access Violations? like what does it mean when it says "Expression cannoit be evaluated" I know what it means in ENGLISH but other wise it just makes no sense since i see that my code is fine and that it compiles fine. Any thoughts?
Can you post some of the code that seems to be causing the problems? (Be sure to use [source] tags...)

Share this post


Link to post
Share on other sites
In the past, I've found the best way to get rid of these kinds of problems is to install an exception handler, and tell the OS to continue execution.

If you're developing in a Windows based environment, the following code can accomplish this...

At the top of your source file containing WinMain, add the function definition:

LONG WINAPI IgnoreAccessViolations (struct _EXCEPTION_POINTERS* exception_info)
{
/* ignore access violations, and continue executing */
return EXCEPTION_CONTINUE_EXECUTION;
}



At the start of your WinMain function, add the following function call:

SetUnhandledExceptionFilter(&IgnoreAccessViolations);



Access violations shouldn't get in your way any more.

--Andrew

Share this post


Link to post
Share on other sites
Quote:
Original post by agottem
In the past, I've found the best way to get rid of these kinds of problems is to install an exception handler, and tell the OS to continue execution.

That is quite possibly the worst advice I've ever read on these forums. And I've seen a lot of bad advice here.

Share this post


Link to post
Share on other sites
Quote:
Original post by agottem
In the past, I've found the best way to get rid of these kinds of problems is to install an exception handler, and tell the OS to continue execution.

If you're developing in a Windows based environment, the following code can accomplish this...

At the top of your source file containing WinMain, add the function definition:
*** Source Snippet Removed ***

At the start of your WinMain function, add the following function call:
*** Source Snippet Removed ***

Access violations shouldn't get in your way any more.

--Andrew

Wow, that's possibly the worst piece of advice I've ever seen. Well done.

Access violations mean your code did something wrong - usually you tried to access a null pointer, or access memory you didn't allocate. Ignoring the problem by sticking your head in the sand and attempting to carry on like nothing happened is not the way to solve it. You need to figure out exactly what is going wrong and fix it properly.

Enerjak: Post code, preferably something complete but minimal. Also try running your code in your debugger and see what happens when it crashes - it should point you to where the code is actually going wrong.

Share this post


Link to post
Share on other sites
Quote:
Original post by agottem
In the past, I've found the best way to get rid of these kinds of problems is to install an exception handler, and tell the OS to continue execution.

If you're developing in a Windows based environment, the following code can accomplish this...

At the top of your source file containing WinMain, add the function definition:
*** Source Snippet Removed ***

At the start of your WinMain function, add the following function call:
*** Source Snippet Removed ***

Access violations shouldn't get in your way any more.

--Andrew

I LOL'd.

Share this post


Link to post
Share on other sites
Another simple answer is: "learn to use the debugger". An access violation will break into the debugger; from there you can examine your variable values and diagnose exactly what went wrong and why.

An Access Violation is a bug in _your_ code that _you_ need to fix. It's not someone else's problem for you to hide or ignore.

-me

Share this post


Link to post
Share on other sites
Ok, i'll post the code and also a picture of the debugger. the thning is, i use the debugger but the numbers just don't make sense. here is the code.



int MODEL::LoadModel(char *filename)
{

LPD3DXBUFFER matbuffer;
HRESULT result;

//load mesh from the specified file
result = D3DXLoadMeshFromX(
filename, //filename
D3DXMESH_SYSTEMMEM, //mesh options
d3ddev, //Direct3D device
NULL, //adjacency buffer
&matbuffer, //material buffer
NULL, //special effects
&material_count, //number of materials
&mesh); //resulting mesh

if (result != D3D_OK)
{
MessageBox(NULL, "Error loading model file", "Error", MB_OK);
return NULL;
}

//extract material properties and texture names from material buffer
LPD3DXMATERIAL d3dxMaterials = (LPD3DXMATERIAL)matbuffer->GetBufferPointer();
materials = new D3DMATERIAL9[material_count];
textures = new LPDIRECT3DTEXTURE9[material_count];

//create the materials and textures
for(DWORD i=0; i<material_count; i++)
{
//grab the material
materials[i] = d3dxMaterials[i].MatD3D;

//set ambient color for material
materials[i].Ambient = materials[i].Diffuse;

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,
&textures[i]);

if (result != D3D_OK)
{
MessageBox(NULL, "Could not find texture file", "Error", MB_OK);
return NULL;
}
}
}

//done using material buffer
matbuffer->Release();

return 1;
}

void MODEL::DeleteModel()
{
//remove materials from memory
if( materials != NULL )
delete[] materials;

//remove textures from memory
if (textures != NULL)
{
for( DWORD i = 0; i < material_count; i++)
{
if (textures[i] != NULL)
textures[i]->Release();
}
delete[] textures;
}

//remove mesh from memory
if (mesh != NULL)
mesh->Release();


}

void MODEL::DrawModel()
{
//draw each mesh subset
for( DWORD i=0; i<material_count; i++ )
{
// Set the material and texture for this subset
d3ddev->SetMaterial( &materials[i] );
d3ddev->SetTexture( 0, textures[i] );

// Draw the mesh subset
mesh->DrawSubset( i );
}
}

int MODEL::PositionModel(int x, int y, int z)
{


X = x;
Y = y;
Z = z;

D3DXVECTOR3 Vec((float)X,(float)Y,(float)Z);

D3DXMatrixTranslation(&MatTrans,x,y,z);

d3ddev->SetTransform(D3DTS_WORLD,&MatTrans);

return 1;
}




it points to "i" as an access violation then there's a "this" violation that is saying that the MatTrans can't be evaluted, and it's saying that all the opther variables can't be evalueted.

here is my model class



class MODEL
{
public:
int LoadModel(char *);
VOID DeleteModel();
void DrawModel();
int PositionModel(int x, int y, int z);

private:
D3DXMATRIX MatTrans;
int X;
int Y;
int Z;
LPD3DXMESH mesh;
D3DMATERIAL9* materials;
LPDIRECT3DTEXTURE9* textures;
DWORD material_count;
};

it's saying all the private members can't be evalueted. so, i hope you can help me. also, it points to line 316 + 0x18 bytes.

EDIT: I seem to have fixed the problem by called MODEL* box = new MODEL()

Ok, i seem to have fixed it, so when dealing with classes i should ALWAYS allocate it with the new keyword?

Share this post


Link to post
Share on other sites
Quote:
Original post by agottem
In the past, I've found the best way to get rid of these kinds of problems is to install an exception handler, and tell the OS to continue execution.

If you're developing in a Windows based environment, the following code can accomplish this...

At the top of your source file containing WinMain, add the function definition:
*** Source Snippet Removed ***

At the start of your WinMain function, add the following function call:
*** Source Snippet Removed ***

Access violations shouldn't get in your way any more.

--Andrew


Holy Bad Advice, Batman! (NEVER do this)

Adding exception handlers in general should really only be used when you want to make some sort of debug output when the exception happens... For example, dumping the call stack once an exception occcurs is an excellent way to trace the problem (especially if you've released this program already and are getting bug reports from users)

Share this post


Link to post
Share on other sites
Ok, there seems to be something wrong with this code.



void Cor_CDirect3D9Renderer::Cor_Sync()
{
HRESULT result = Cor_Direct3DDevice9->Clear(0,0,D3DCLEAR_TARGET,D3DCOLOR_XRGB(255,0,0),1.0f,1);

if(FAILED(result))
{
return 0;
}

if(Cor_Direct3DDevice9->BeginScene())
{
Cor_Direct3DDevice9->EndScene();
}

Cor_Direct3DDevice9->Present(NULL,NULL,NULL,NULL);
}




I see nothing wrong with this. but who knows. I also put this in the main loop in WinMain because i found out that things don't have to be places exactly in between begin/endScene you can draw everything as long as you can a function that updates the screen.
Anyways, when i take out the Cor_Sync() it works fine. but yeah, i need that to render so any advice would be helpfull. thanks

Share this post


Link to post
Share on other sites
0x00000000000. it just doesn't seem to like the variable. I don't know why. What can it be done with this? also it says Line 45 + 0x21. Anything i can do? cause Cor_Direct3DDevice9 is only giving me problems in that function.

Share this post


Link to post
Share on other sites
Quote:
Original post by Enerjak
0x00000000000. it just doesn't seem to like the variable. I don't know why. What can it be done with this? also it says Line 45 + 0x21. Anything i can do? cause Cor_Direct3DDevice9 is only giving me problems in that function.



That would be a NULL pointer.


Quote:
Access violations shouldn't get in your way any more.


A: Sir, klaxons are going off, red lights are blinking, and the nuclear reactor is making strange noises!!!
B: There's a switch at the back, that will turn off these annoyances, flip it

* The above is loose transcription of the discussion between two technicians in Chernobyl power plant. And yes, it worked perfectly. A few minutes later, everything was quiet. It remains quiet to this day.

Share this post


Link to post
Share on other sites
I take it your either telling me i'm a dumb ass or something else. i can't quite make it out. Well, what ever it is, i'll figure it out. gotta.

OH wait you were talking to the other guy

[Edited by - Enerjak on January 30, 2008 9:14:35 PM]

Share this post


Link to post
Share on other sites

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