9

I tried using the dispose function on a texture2d but that caused problems and I'm pretty sure it's not what I'm meant to use.

What should I use to basically unload content? Does the content manager keep track itself or is there something I have to do?

Brian Rasmussen
  • 114,645
  • 34
  • 221
  • 317
meds
  • 21,699
  • 37
  • 163
  • 314
  • possible duplicate of [How does XNAs Content.Load operate?](http://stackoverflow.com/questions/4242741/how-does-xnas-content-loadtexture2d-operate) – Neil Knight Nov 24 '10 at 10:34

2 Answers2

12

Take a look at my answers here and possibly here.

The ContentManager "owns" all the content that it loads and is responsible for unloading it. The only way you should unload content that a ContentManager has loaded is by using ContentManager.Unload() (MSDN).

If you are not happy with this default behaviour of ContentManager, you can replace it as described in this blog post.

Any textures or other unload-able resources that you create yourself without going through ContentManager should be disposed (by calling Dispose()) in your Game.UnloadContent function.

Community
  • 1
  • 1
Andrew Russell
  • 26,924
  • 7
  • 58
  • 104
  • There is also a ContentManager.Unload(bool disposing) which is described as also unloading managed content if true. Are there xna content types in the xna library that are managed and need manual disposing? – Wouter Apr 18 '12 at 10:30
  • 1
    @Wouter Unload doesn't take an argument in any version I see on MSDN. You might be looking at `Dispose(bool disposing)` (which is a protected method, can't call externally). This is part of the standard C# disposable pattern ([explained here](http://msdn.microsoft.com/en-us/library/fs2xkftw.aspx)). `Dispose(true)` gets called during standard disposal (the `using` statement or directly calling `Dispose()`) and will itself call `Unload()`. `Dispose(false)` gets called by the finalizer and specifically does not call `Unload` (it can't access other objects, they might have already been finalized). – Andrew Russell Apr 21 '12 at 15:27
1

If you want to dispose a texture, the easiest way to do it:

    SpriteBatch spriteBatch;
    Texture2D texture;
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        texture = Content.Load<Texture2D>(@"Textures\Brick00");
    }
    protected override void Update(GameTime gameTime)
    {
        // Logic which disposes texture, it may be different.
        if (Keyboard.GetState().IsKeyDown(Keys.D))
        {
            texture.Dispose();
        }

        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);
        spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.LinearWrap, DepthStencilState.None, RasterizerState.CullCounterClockwise, null);

        // Here you should check, was it disposed.
        if (!texture.IsDisposed)
            spriteBatch.Draw(texture, new Vector2(resolution.Width / 2, resolution.Height / 2), null, Color.White, 0, Vector2.Zero, 0.25f, SpriteEffects.None, 0);

        spriteBatch.End();
        base.Draw(gameTime);
    }

If you want to dispose all content after exiting the game, the best way to do it:

    protected override void UnloadContent()
    {
        Content.Unload();
    }

If you want to dispose only texture after exiting the game:

    protected override void UnloadContent()
    {
        texture.Dispose();
    }
Wallstrider
  • 856
  • 1
  • 7
  • 22