title: DirectX Release function with Textures (C++)
body: <p>I have a question regarding DirectX 9, textures and releasing them.
  I'm currently building a 2D game in C++.  I have many textures that I'm loading with D3DXCreateTextureFromFileEx, and then when moving on from that 'scene' releasing them with Texture->Release(); which is fine.</p>
  
  <p>I've made a class which deals with the game's item list.  In it, I'm loading textures for each of the items when the class is created, and when the game exits it releases each of those textures.  However, I keep running into a problem.</p>
  
  <p>Even though I have already loaded the texture, if I do not ->draw() it, the program will crash when trying to release it.</p>
  
  <p>Can anyone explain why this is please?
  I imagine I will need some sort of conditional check to see if the texture has been drawn before I attempt to ->release() it?</p>
  
  <p>EDIT:</p>
  
  <p>Here's a much watered down version of what I was trying to describe.  This works, but if you comment out the d3dspt->Draw line, it fails when releasing the texture.  It appears to only be able to release textures once they have been drawn.  I assumed you would need to release them after you've loaded and finished with them, regardless if they have been drawn or not.  I was just looking for a confirmation, explanation or anything else that might enlighten me.
  For now, I have added a boolean to each texture to check whether it's been drawn or not when I come to releasing it.</p>
  
  <pre><code>LPDIRECT3DTEXTURE9 Texture;
  
  D3DXCreateTextureFromFileEx(d3ddev,
                          L"Texture.png",
                          D3DX_DEFAULT,
                          D3DX_DEFAULT,
                          D3DX_DEFAULT,
                          NULL,
                          D3DFMT_A8R8G8B8,
                          D3DPOOL_MANAGED,
                          D3DX_DEFAULT,
                          D3DX_DEFAULT,
                          NULL,
                          NULL,
                          NULL,
                          &amp;Texture);
  
  d3ddev-&gt;Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
  d3ddev-&gt;BeginScene();
  d3dspt-&gt;Begin(D3DXSPRITE_ALPHABLEND);
  
  D3DXVECTOR3 TextureCentre(0.0f, 0.0f, 1.0f);
  D3DXVECTOR3 TexturePos(0.0f, 0.0f, 1.0f);
  
  d3dspt-&gt;Draw(Texture, NULL, &amp;TextureCentre, &amp;TexturePos, D3DCOLOR_ARGB(255, 25, 255, 255));
  
  d3dspt-&gt;End();
  d3ddev-&gt;EndScene();
  d3ddev-&gt;Present(NULL, NULL, NULL, NULL);
  
  Texture-&gt;Release();`
  </code></pre>