Sign in to follow this  
ccanan

Texture Release Problems

Recommended Posts

when I release mipmaped texture, need I traverse all the surface and release them?or just call: texture->release(); this puzzle comes from dx sdk on the [Texture Filtering with Mipmaps]page. it provides a example: -------------------------------------------------------------------------- IDirect3DSurface9 * pSurfaceLevel; for (int iLevel = 0; iLevel < pMipMap->GetLevelCount(); iLevel++) { pMipMap->GetSurfaceLevel(iLevel, &pSurfaceLevel); // Process this level pSurfaceLevel->Release(); } --------------------------------------------------------------------------- and some src I download from internet only release the texture directly. so what is right? thx in advance.

Share this post


Link to post
Share on other sites
All the DX interfaces are MS COM interfaces; this standard defines how interfaces can be obtained and released. The basic rule is that you need to release any interface you obtain. In your case: if you do not obtain explicit interfaces to a texture's mipmaps, you don't have to release those.

For example: using just the texture without accessing its surfaces:

IDirect3DTexture9* texture = NULL;
device->CreateTexture( ..., &texture );

// do stuff

texture->Release();
texture = NULL;


For example: using the texture and its mipmaps:

IDirect3DTexture9* texture = NULL;
device->CreateTexture( ..., &texture );

IDirect3DSurface9 * pSurfaceLevel;
for (int iLevel = 0; iLevel < texture ->GetLevelCount(); iLevel++)
{
texture->GetSurfaceLevel(iLevel, &pSurfaceLevel);
// Process this level
pSurfaceLevel->Release();
}

texture->Release();
texture = NULL;


So for each call to GetSurfaceLevel(), which obtains an IDirect3DSurface9 interface, you need to call Release() on the interface. As long as Create/Get/AddRef and Release come in pairs you're ok.

Greetz,

Illco

Share this post


Link to post
Share on other sites
I think the release means:
1st: refCount--;
2nd: if(refCount==0) delete obj;
right?

my question is :if I just release the texture when there is 1 ref to the texture(so it's deleted), will all the mipmap be released(deleted) either?

Share this post


Link to post
Share on other sites
Quote:
Original post by ccanan
I think the release means:
1st: refCount--;
2nd: if(refCount==0) delete obj;
right?

Yes. Kind of at least (i.e. I don't know what else they do but this is the right way to think about it).

Quote:
Original post by ccanan
my question is :if I just release the texture when there is 1 ref to the texture(so it's deleted), will all the mipmap be released(deleted) either?

[/quote]
Well: no if you didn't obtain their interfaces (through GetSurfaceLevel() or other); otherwise (usual case) yes.

Illco

Share this post


Link to post
Share on other sites
I think you're trying to work too hard, ccanan. If you didn't ever access the mipmap surfaces directly, they will take care of themselves. A good rule of thumb is to call Release() for each object you Create(), Get() or Load(). On destruction, a COM object will release everything it created, so unless you made it your business at any point, there's nothing to lose sleep over.

If you're unsure what to release and what not to, then run your app through under the debug runtime. Any attempts to release a dead object will give a runtime error, and any unreleased objects upon termination will be reported as a memory leak.

Regards
Admiral

Share this post


Link to post
Share on other sites
thx for your replies.
And I didn't use the debugger that much, sure I should check it with debugger before I post this thread.
^_^

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