Original Post
Does the IDirect3DSurface8::GetDesc function increase the refrence count. And if not.. which one of these does?
Here''s a code snippet... why am i getting one extra reference count on the TSurface object?
HRESULT hr=0;
LPDIRECT3DTEXTURE8 Heightmap=0;
LPDIRECT3DSURFACE8 TSurface=0;
BYTE *image=0;
try{
hr = D3DXCreateTextureFromFileA(g_pd3dDevice,filename,&Heightmap); // Load a graphics file..
if (!Heightmap)
throw "Can''t open file";
// Get a surface from the texture.. so we can mnipulate it
hr = Heightmap->GetSurfaceLevel(0,&TSurface);
if (!TSurface)
throw "Can''t get Surface!";
D3DLOCKED_RECT lockedr;
D3DSURFACE_DESC desc;
hr = TSurface->GetDesc(&desc); // Get the sizes etc of the surface
hr = TSurface->LockRect(&lockedr,0,D3DLOCK_READONLY); // Lock entire surface so we can use it
// Pitch is the amount of bytes in a line of graphics data.
// eg. a ptr Data is the start of the graphics data.
// Data+pitch is the next line of pixels
// Data+(pitch*2) is the second line of pixels
int pitch=lockedr.Pitch;
// I''m unsure wether Unlocking the surface invalidates the pBits ptr. so i copy it to my own ptr.
BYTE *bits = (BYTE*)lockedr.pBits;
image = new BYTE[desc.Size];
memcpy(image,bits,desc.Size);
hr = TSurface->UnlockRect(); // Unlock surface coz we don''t need it anymore
// We don''t need the surfaces or heightmap texture anymore.. so let''s get rid of them.
hr = TSurface->Release(); // Free surface
// This is just a debug-test routine... the returned value from ->Release() is the reference count
// of it''s use. Now in this function it''s only accessed once and therfore *should* be 0 once i
// release it... But it keeps comming up as 1 when i use my debugger.. one day i''ll find out why.
// For now though.. i''ll just release it twice
if (hr != 0)
{
DText("Surface Reference Count not 0: It''s %d",hr);
hr = TSurface->Release(); // Free surface
}
TSurface = 0; // set ptr to 0
hr = Heightmap->Release(); // Decrease Reference Count from GetSurfaceLevel
hr = Heightmap->Release(); // Free texture
Heightmap = 0; // Set ptr to 0