Skip to main content
gamedev.net
🔒 Locked

Reference problem with ->Release()

Started by Spikeles Apr 15, 2002 at 9:12 PM 2 replies 500+ views
Original Post
Spikeles
Spikeles
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
 
jaxson
jaxson
I would guess that Heightmap also has a reference to it. When you ask for the surface level, it AddRef()s and then returns you the pointer. That way, when you release the surface, Heightmap still has it.

I also am not sure why you are releasing Heightmap twice since it looks like you should only have one reference to it as far as I can tell.
Spikeles
Spikeles
That''s exactly why i relase it twice. Once to decrease the refernce of GetSurfaceLevel() and once to free the actual memoery for Heightmap.

But why is TSurface still have 1 extra refernce?

This is how i understand what is happening.
1. Ok. i make a heightmap
heightmap ref = 1
2. i get the surface level
heightmap ref = 2
Tsurface ref = 1
3. i lock the surface
4. i unlock the surface
5. I release the TSurface
Tsurface = 0
TSurface is now freed. should be no more refernces
7. I relase the heightmap to free the call from getsurface level
heightmap ref = 1
8. I relase the heigght map once more..
heightmap ref = 0
jaxson
jaxson
You don't need to release the heightmap twice. You need to release the surface once to give up your reference to it. However, the heightmap will still have a reference to the surface (that's why you get a return value of one). Then, when you release your height map (which you only need to do once) it will internally release the other reference to the surface.

That will mean a total of 2 releases. One release of the surface and one release of the heightmap. If you want to verify this you could try an extra addref on the surface, release the surface and the heightmap once each and then release the surface one more time and see if you are back at zero. (You should remember though that you shouldn't completely rely on the reference count value returned by AddRef and Release because you never know what is going on behind the scenes as you can see by this problem here.)

[edited by - jaxson on April 15, 2002 12:47:40 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.