views:

74

answers:

1

Hi,

I am very new to XNA framework. I am writing a sample application in XNA for windows phone 7.

presently I am facing a problem.

In the sample, i am loading a Texture2D and dispose it in the next line and assign it to null. Again I load the same image to the same member variable. But in the draw I get ObjectDisposedException.

If I remove the dispose call it will not give any exception.

Please help me to solve this.

Sample:

Texture2D texture = null;
 protected override void LoadContent()
 {
      texture = Content.Load<Texture2D>("Back");
      texture .Dispose();
      texture = null;

      texture = Content.Load<Texture2D>("Back");
}


protected override void Draw(GameTime gameTime)
{
      GraphicsDevice.Clear(Color.CornflowerBlue);

      spriteBatch.Begin();
      spriteBatch.Draw(texture , new Vector2(0, 0), Color.White);

      spriteBatch.End();

       base.Draw(gameTime);
}
+4  A: 

The ContentManager that you are using automatically manages the lifetime of assets. It caches the "Back" texture after the first call and returns the same instance the second time you ask for it. Unfortunately you have asked the Texture to dispose itself so it is no longer in a usable state.

You can use Content.Unload to remove the texture from memory.

Technium
Keep in mind that Content.Unload unloads all of the resources in the content manager. You can't use it to unload a specific resource.
Crappy Coding Guy