Sign in to follow this  

crash on texture-LockRect!! help

This topic is 4491 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

I encounter an "0xC0000005: Access violation reading location 0xdeadbf3b." during m_pTexture->LockRect I knw what access vialation is but the problem is all of my pointers are valid(including m_pTexture) here are the summary for my pointers during the crash m_pTexture 0x00ab8c40 heres the function
DWORD CTexture::GetColor( int x,int y )
{
	DWORD color;
	D3DLOCKED_RECT rect;
	HRESULT hr;

	hr = m_pTexture->LockRect(0,&rect,NULL,D3DLOCK_NOSYSLOCK);
	DWORD* bits = (DWORD*)rect.pBits;
	int offset = ((rect.Pitch/sizeof(DWORD)) * y) + x;
	color = bits[offset];
	m_pTexture->UnlockRect( 0 );
	return color;
}



and the m_pTexture is loaded from file with this function and creation of the texture returns TRUE Note: m_pTexture is initialize during the constructor
HRESULT CTexture::LoadEx( LPDREXGRAPHICS Gfx, std::string szPath, DWORD colorkey,  D3DFORMAT format )
{
	m_pGraphics = Gfx;
	m_szFileName = szPath;
	if(FAILED( D3DXCreateTextureFromFileEx(m_pGraphics->GetDevice( ),szPath.c_str(),D3DX_DEFAULT,D3DX_DEFAULT,
											D3DX_DEFAULT,0,format,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,colorkey,
											NULL,NULL,&m_pTexture)))
			return FALSE;
//	m_pTexture->GetSurfaceLevel(0, &m_Surface);
	return TRUE;



heres how i call the function above m_texture.LoadEx(m_pGraphics,szTexture,0,D3DFMT_X8R8G8B8); u guys got any idea whats wrong with this ? ill gave another information if the one i post doesnt really describe the problem Edit: Ive discovered this when i place the body of the Getcolor function to my LoadEx everything works fine and lockrect doesnt crash. whats the logical explanation for this? see bellow
HRESULT CTexture::LoadEx( LPDREXGRAPHICS Gfx, std::string szPath, DWORD colorkey,  D3DFORMAT format )
{
	m_pGraphics = Gfx;
	m_szFileName = szPath;
	if(FAILED( D3DXCreateTextureFromFileEx(m_pGraphics->GetDevice( ),szPath.c_str(),D3DX_DEFAULT,D3DX_DEFAULT,
											D3DX_DEFAULT,0,format,D3DPOOL_MANAGED,D3DX_DEFAULT,D3DX_DEFAULT,colorkey,
											NULL,NULL,&m_pTexture)))
			return FALSE;

	DWORD color;
	D3DLOCKED_RECT rect;
	HRESULT hr;

	m_pTexture->LockRect(0,&rect,NULL,D3DLOCK_READONLY);
	DWORD* bits = (DWORD*)rect.pBits;
	int offset = ((rect.Pitch/sizeof(DWORD)) * 0) + 0;
	m_pTexture->UnlockRect( 0 );

[Edited by - yuppies on August 31, 2005 4:16:53 AM]

Share this post


Link to post
Share on other sites
Are your x and y coordinates still inside the textures rect?

Remember, D3DXCreateTextureFromFileEx may also increase the given image size to the next power of 2. Check this with calling GetSurfaceDesc.

Could happen if you've got a buggy driver (a 3dfx one did this to me once): D3DXCreateTextureFromFile actually created a texture SMALLER then the size i wanted it to be.

The crash address also looks particularely close to 0xdeadbeef which is a value the debugger fills into certain memory areas.

Share this post


Link to post
Share on other sites
I noticed the deadbeef part to, strange it comes so close. Anyway, what values do you supply for x and y? If they are coordinates of the texture you may be wrong. In the following line:

int offset = ((rect.Pitch/sizeof(DWORD)) * y) + x;

you implicitly assume each pixel is stored as a byte (since you next use this offset to index an array of texture bytesnope you're right its DWORD. However how do you know each pixel is stored as a DWORD-sized element? Usually it is, perhaps always, just checking.).

You are also assuming that the texture is in its most explicit form (i.e. N times N color values) which is also often not the case (with A8R8B8G8 you can be sure).

Then LockRect() has many prerequisites and possible causes of error; consult the SDK documentation for this. Make sure you check every returned result/error value and look it up -- especially the one returned from LocktRect() of course!

Greetz,

Illco

Share this post


Link to post
Share on other sites
hi

i commented some of the lines and still i encounter the same crash
i rebuild the application more than once to make sure the changes are made

m_pTexture->LockRect(0,&rect,NULL,D3DLOCK_READONLY);
//DWORD* bits = (DWORD*)rect.pBits;
//int offset = ((rect.Pitch/sizeof(DWORD)) * y) + x;
//color = bits[offset];
m_pTexture->UnlockRect( 0 );
]

strangely , like what ive said on the post above that when i tranfer the code AFTER the loading of the image everything works fine,

if its ok with you guys can you show me the right way how to do it? i mean loading a texture file and retrieving the color at the particular x,y position

thanks

Share this post


Link to post
Share on other sites
The reason that works is you copied '0' for x and '0' for y, effectively only indexing the very first byte. It seems logical the errors occur at the boundaries of the texture memory rather than in the first byte. See:

int offset = ((rect.Pitch/sizeof(DWORD)) * 0) + 0;


I hardly ever need to touch texture pixels. If so it is in a loading phase in which efficiency can be disregarded (well a bit at least). Then I make a copy of the top-level surface onto a surface of which I know it is in A8R8B8G8 format and of which I know it is lockable -- this surface I can then access much like your code. After modification the surface is copied back.

This is how it is done in for example EffectEdit as well, have a look at the code over there.

Greetz,

Illco

Share this post


Link to post
Share on other sites

This topic is 4491 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.

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