Advertisement Jump to content
Sign in to follow this  

[C++, DX9] Access violation in writing to frame buffer

This topic is 3108 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have spent 5 hours debugging this, and I really ran out of ideas. I appreciate any help you can give, thanks.

I am trying to program my own software renderer. It runs perfectly fine at first, until I did the following:
a) Toggle to full screen.
b) Then toggle back to windowed mode.

which it crashes immediately right after I toggle back to windowed mode, and Visual studio gives the following message:
- Unhandled exception at 0x00039d25 in SoftwareRenderer.exe: 0xC0000005: Access violation writing location 0x03a80000.

What I did is to store the color of each pixel in an array (let's call it the FB array), and then copying it to the frame buffer in each frame. Specifically, each frame I did the following steps:

1) Clearing the contents of the FB array.
2) Do my own rendering modifying the FB array.
3) Lock the frame buffer and get the access to its memory location.
4) Copy from FB array to the frame buffer pixel by pixel.
5) Unlock the frame buffer.
6) Present the frame buffer to screen.

The error occurs at step 4), where I copy from FB array to the frame buffer. I have made sure that I reset the device correctly (ie, releasing resources before resetting device), and check that the device is valid (using TestCooperative()). Below is the code for the RenderFrame() that's called on each frame update.

memset(FB.GetImage().buf, 0, FB.GetImage().buf_size);


TDE(d3d_device->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &back_buffer));


D3DLOCKED_RECT locked_rect_back_buffer;
TDE(back_buffer->LockRect(&locked_rect_back_buffer, NULL, 0));

int linear_pitch = locked_rect_back_buffer.Pitch;

USHORT* surface_buffer = static_cast<USHORT*>(locked_rect_back_buffer.pBits);
linear_pitch >>= 1;

for (int y = 0; y < screen_height; y++) {
for (int x = 0; x < screen_width; x++) {
surface_buffer[x + y * linear_pitch] = *((USHORT*)FB.GetPixelAt(x, y));


back_buffer = NULL;


if (FAILED(d3d_device->Present(NULL, NULL, NULL, NULL)))
device_lost = true;

Thanks for reading and thanks in advance for your help !!!

[Edited by - ani83 on July 21, 2010 11:35:29 AM]

Share this post

Link to post
Share on other sites
I have finally solved the problem !!!

To anyone who might find this information useful:

What I did is just change from the following:

if (window_mode) {
d3dpp.BackBufferWidth = 0;
d3dpp.BackBufferHeight = 0;


if (window_mode) {
d3dpp.BackBufferWidth = screen_width;
d3dpp.BackBufferHeight = screen_height;

I don't know why it works fine first in windowed mode, it only crashes when I toggle to full screen and back to windowed mode. If you have an explanation for this, I would love to know, thanks.

Share this post

Link to post
Share on other sites
What is screen_width and screen_height? And what is locked_rect_back_buffer.pBits, compared to the access violation address (I.e. how far ahead of the start of the lock does the access violation occur)?

If you call GetDesc() on the back_buffer, is it definitely the size you think it is?

Share this post

Link to post
Share on other sites
screen_width and screen_height is the horizontal and vertical screen resolution (which I set to 800 and 600 respectively, and the values do not change throughout the program).

I've used the GetDesc() to obtain the frame buffer's Width and Height. It outputs 800 and 600. However when I toggle to full screen and back to windowed mode again, the Height value changed from 600 to 566, which explains the memory access violation.

Share this post

Link to post
Share on other sites
Before you create your window, do you call AdjustWindowRect so the window is created with the desired client size?

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!