Sign in to follow this  
BryanSpence

Access violation reading location

Recommended Posts

BryanSpence    122
Hi all, I had my code in a reasonable condition when, what seemed out of nowhere I got the following error; First-chance exception at 0x0047e917 in example1.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd. Unhandled exception at 0x0047e917 in example1.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd. The program '[1000] example1.exe: Native' has exited with code 0 (0x0). The problem appears to be with the following code; sprite_handler->Draw( car_image, NULL, NULL, &position, D3DCOLOR_XRGB(255,255,255)); Actually it points to the last line, D3DCOLOR_XRGB(255,255,255)); I'm making a game with C++ and DirectX 9.0 BTW. I haven't touched this code in ages and from what I see there isn't anything wrong with it. So far all I've done is try rebuild the project without sucess. Any thoughts, please!!!

Share this post


Link to post
Share on other sites
LessBread    1415
0x0047e917 - this is the address of the faulting instruction. It's somewhere in your code.

0xcdcdcdcd - this is the address of the location to be read. Trying to read addresses above 0x80000000 will always throw. This is a debugging value anyway [1].

Quote:

0xCDCDCDCD is a debugging value that comes from the bowels of the C runtime library. When you allocate a block of memory in a debug build, it's initialized to this spurious value in the hope of catching bugs. 0xCDCDCDCD is non-NULL and is never a valid memory pointer.
...

Share this post


Link to post
Share on other sites
__ODIN__    479
Sounds like a pointer error. Try checking that the sprite_handler and car_image are valid pointers. Also, make sure you reset any pointers to NULL when you create them (or in the constructor).

Allan

Share this post


Link to post
Share on other sites
BryanSpence    122
I tried setting all the pointers to NULL in the constructor but that made the images not appear! The background did however and I was able to move around as if the car was there.

I never set them to NULL in the constructor before and worked without it.

sprite_handler and car_image seem to be valid pointers, they are declared in the header file as follows;

LPDIRECT3DTEXTURE9 car_image;
LPD3DXSPRITE sprite_handler;

Thanks for the quick responce!

Share this post


Link to post
Share on other sites
Evil Steve    2017
sprite_handler is an invalid pointer. It's declared, yes. But it's not created. You need to use D3DXCreateSprite to create it.

The reason the compiler breaks on the last line is that it's only one function call. So it breaks at the end of it.

Share this post


Link to post
Share on other sites
BryanSpence    122
I do have that code;

hr = D3DXCreateSprite(pd3dDevice9, &sprite_handler);
if (hr != D3D_OK)
return 0;
So thats not the problem.

There are also lots of function calls for each of the sprites. If I were to comment out the function call for car_image it would find the same problem with the next sprite, which would barrier (declared the same way as car_image)

Share this post


Link to post
Share on other sites
Evil Steve    2017
Use the debugger's variable watch window to look at the value of sprite_handler when you get the access violation. I'm still betting it's 0xcdcdcdcd. What happens if you put the call to D3DXCreateSprite just before the Draw() call? Does it work then?

Share this post


Link to post
Share on other sites
BryanSpence    122
The D3DXCreateSprite would be called before the draw function anyway using a function called InitDirectX. I put it directly before the draw function anyway and still no joy.

Share this post


Link to post
Share on other sites
DXnut    262
Did you neglect to call the ID3DXSprite::Begin and ::End functions? Are you sure that you correctly loaded the texture file into car_image?

You should also check the HRESULT with:

if (FAILED(hr))

or

if (SUCCEEDED(hr))

Share this post


Link to post
Share on other sites
BryanSpence    122
Good news, I changed the location of the D3DXCreateSprite function call and by magic and the grace of god it works again.

Just for the record, I do actually have the Begin and End functions.

Also when I checked the vlaues during debugging I found that sprite_handler had a normal value while car_image, barrier etc had the 0xcdcdcdcd value.

Thank you very much to everyone who helped me, I REALLY appreciate it.

Share this post


Link to post
Share on other sites
__ODIN__    479
Quote:
Original post by BryanSpence
Good news, I changed the location of the D3DXCreateSprite function call and by magic and the grace of god it works again.
.


Danger, Will Robinsons... Danger.

If you don't understand WHY it now works, please pause the spaceship and examine the code until you do. Failure to do so will result in you being mobbed by big, scary space BUGS.

It most likely failed because you
a) Didn't initialize the textures or
b) Called the Draw function before you initalized the textures.

Setting pointers to NULL is a good idea because:
a) in Release mode, VC won't help you out by resetting memory to 0xCDCDCDCD
b) you can verify pointers before you use them:

if (car_image)
{
sprite_handler->Draw(car_image, NULL, NULL,&position,D3DCOLOR_XRGB(255,255,255));
}


Share this post


Link to post
Share on other sites
Evil Steve    2017
I completely agree with __ODIN__. Also, install the debug runtimes and link with d3dx9d.lib in debug builds! If you had them installed, then D3DX would validate the pointers, realise it wasn't valid and let you know in the debug output.

Your variable isn't getting initialized, either because you're not creating it, or you're releasing it somehow (Which I don't think is the case, because then the debugger would use a value other than 0xcdcdcdcd).

You really need to find out what's wrong here. Not doing anything will just cause huge problems later on when another part of your code breaks and suddenly this bug pops up again.

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