0

Error: Cannot access a disposed object. Object name: 'Texture2D'

Why is this happening! Nowhere in the whole of my code does it tell it to dispose! This is occuring on the LoadContent() in my XNA c# game. Code where i get this error, ive highlighted the Texture2D were this occurs.

 protected override void LoadContent()
    {
        SoundEffect.DistanceScale = 20;
        SetUpPlain();
        SetUpWalls();
        // Walls
        wallsEffect = new BasicEffect(graphics.GraphicsDevice);
        wallsEffect.World = Matrix.Identity;
        wallsEffect.Projection = vars.projection;
        wallsEffect.TextureEnabled = true;
        wallsEffect.Texture = Content.Load<**Texture2D**>("Textures\\Fence");
        // Set-up Player Options
        Player1.PlayerModel = Content.Load<Model>("PlayerA");
        Player1.BoundingSphere = new BoundingSphere(new Vector3(Player1.Position.X, Player1.Position.Y - 2, Player1.Position.Z), Player1.PlayerModel.Meshes[0].BoundingSphere.Radius * 0.04f);
        Player2.PlayerModel = Content.Load<Model>("PlayerA");
        Player2.BoundingSphere = new BoundingSphere(new Vector3(Player2.Position.X, Player2.Position.Y - 2, Player2.Position.Z), Player2.PlayerModel.Meshes[0].BoundingSphere.Radius * 0.04f);
        //
        loadStaticModel(Content, "Radio", null, 11, 4, -2, 0, 180, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 10, 1, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 12, 1, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "AmmoCrate", null, 11, 3, 0, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Forklift", null, 15, 0, -5, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "PepsiMachine", null, 25, 0, -5, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Chest", null, 10, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "PalletBoard", "Cork", 10, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Table", "Cork", 15, 0, -20, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Cylinder", "Cork", 15, 0, -40, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 184, 0, -196, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "WallTop", "Cork", 184, 0, -188, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 184, 0, -180, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Door1", "MetalA", 184, 0, -188, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Door1", "MetalA", 184, 0, -184, 0, 0, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 188, 0, -176.15f, 0, 90, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        loadStaticModel(Content, "Wall", "Cork", 196, 0, -176.15f, 0, 90, 0, CollisionType.Default, LightingType.AmbientDirectional, true, false);
        for (int i = 1; i <= MapSize / 8; i++)
        {
            loadStaticModel(Content, "Fence", "Fence", i * 8.12f - 4, 0, 0, 0, 270, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", 0, 0, -(i * 8.12f - 4), 0, 180, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", MapSize, 0, -(i * 8.12f - 4), 0, 0, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
            loadStaticModel(Content, "Fence", "Fence", i * 8.12f - 4, 0, -MapSize, 0, 90, 0, CollisionType.None, LightingType.AmbientDirectional, false, false);
        }
        // Sound
        vars.soundEffect[0] = Content.Load<SoundEffect>("Sounds\\NormalStep");
        vars.soundEffect[1] = Content.Load<SoundEffect>("Sounds\\Enter Sandman");
        vars.soundEffect[2] = Content.Load<SoundEffect>("Sounds\\PlayerJoinGame");
        vars.soundEffect[3] = Content.Load<SoundEffect>("Sounds\\JumpLand");
        vars.soundEffectInstance[0] = vars.soundEffect[1].CreateInstance();
        soundEffectPosition[0] = new Vector3(0, 200, 200);
        soundEffectEmitter[0] = new AudioEmitter();
        soundEffectEmitter[0].Position = soundEffectPosition[1];
        soundEffectListener[0] = new AudioListener();
        soundEffectListener[0].Position = Player1.Position;
        vars.soundEffectInstance[0].Apply3D(soundEffectListener[0], soundEffectEmitter[0]);
        vars.isSoundPlaying[0] = false;
        // Menu's
        vars.menuBackground = Content.Load<Texture2D>("Textures\\menuBackground");
        PauseMenu.screenShotHighlight = Content.Load<Texture2D>("Textures\\ScreenShotViewerHighlight");
        vars.MenuFont = Content.Load<SpriteFont>("Fonts//MenuFont");
        // Death HUD
        deathBackground = Content.Load<Texture2D>("Textures\\deathBackground");
        //
        chatBackground = Content.Load<Texture2D>("Textures\\chatBackground");
        plainTexture = Content.Load<Texture2D>("Textures\\Grass");
        // Tank
        tank = Content.Load<Model>("tank");
        tankTransforms = new Matrix[tank.Bones.Count];
        // Sky Dome
        skyDome = Content.Load<Model>("SkyDome");
        skyDomeTexture = Content.Load<Texture2D>("Textures\\skyBox");
        skyDomeTransforms = new Matrix[skyDome.Bones.Count];
        //
        font = Content.Load<SpriteFont>("Fonts//PCFont1");
        chatFont = Content.Load<SpriteFont>("Fonts//chatFont");
        xboxButtons = Content.Load<SpriteFont>("Fonts//xboxControllerSpriteFont");
        xboxFont = Content.Load<SpriteFont>("Fonts//MenuFont");
        spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
        TextureEffect = Content.Load<Effect>("TextureEffect");
        // Viewports
        leftViewport = GraphicsDevice.Viewport;
        rightViewport = GraphicsDevice.Viewport;
        leftViewport.Width = leftViewport.Width / 2;
        rightViewport.Width = rightViewport.Width / 2;
        rightViewport.X = leftViewport.Width;
        // 
    }
  • Do you call Dispose() on your ContentManager? Do you use a custom content processor/loader of some sort? Do you Dispose() anything else, such as a Model? Is there *anything* in your program that you Dispose(), and that could call through to that texture? Also, what is the stack trace of the error message? All I see is loading code -- does it fail there? The first time you run the code, or some future time? – Jon Watte Dec 18 '11 at 22:25
  • I NEVER call Dispose() and im not using a custom content processor/loader. How can i get the stack trace? – Reece Aaron Lecrivain Dec 18 '11 at 22:35
  • Is the exception thrown right when you attempt to load the Texture2D? what is the exception? you can surround the code in LoadContent in a try...catch and send us the exception that is caught there. – lysergic-acid Dec 19 '11 at 06:44
  • Fixed it! I was accidentally calling LoadContent() when it had already been loaded. – Reece Aaron Lecrivain Dec 19 '11 at 12:45
  • You should post that as an answer so it can be seen that the question has been answered. – Kevin Reid Dec 19 '11 at 15:26

1 Answers1

0

skyDomeTexture = Content.Load("Textures\skyBox");

try this

skyDomeTexture = Content.Load(@"Textures/skyBox");

Bixel
  • 75
  • 6
  • You're verbatim string literal should still have a back-slash. The first example would escape the string as \s being a space, while the second example would not attempt to escape the s. – Phill Dec 21 '11 at 03:04