-1

The below code gives me a ObjectDisposedException. I load the contents in the content loader just fine. It works from time to time but occasionally it throws the exception.

All textures are loaded perfectly and each and every object has the right Texture and actually exists. There is nowhere a line where I unload content.

The error is thrown at the last spriteBatch.End();

What goes wrong?

 protected override void Draw(GameTime gameTime)
        {
            if (startMenu.showMenu)
            {
                spriteBatch.Begin();
                startMenu.Draw(spriteBatch);
                spriteBatch.End();
            }
        if (selectedLevel != null && !startMenu.showMenu)
        {
            spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null,
            selectedLevel.GetCamera().getViewMatrix(new Vector2(1.0f)));
            for (int layer_inc = 0; layer_inc <= selectedLevel.layers.Count() - 1; layer_inc++)
            {
                selectedLevel.layers[layer_inc].Draw(gameTime, spriteBatch);
            }
            if (selectedLevel.GetPlayer().IsAlive())
            {
                Weapon weapon = selectedLevel.GetPlayer().GetWeapon();
                weapon.Draw(gameTime, spriteBatch);

                if (selectedLevel.GetPlayer().isMeditating())
                {
                    Player player = selectedLevel.GetPlayer();
                    MeditationBoost meditationBoost = ((MeditationBoost)player.getBoost());
                    meditationBoost.Draw(spriteBatch, player.position);
                }
                if (weapon is ShootableWeapon)
                {
                    ShootableWeapon shootableWeapon = (ShootableWeapon)weapon;

                    if (shootableWeapon.getAmmo().Count() > 0)
                    {
                        for (int ammo_inc = 0; ammo_inc <= shootableWeapon.getAmmo().Count() - 1; ammo_inc++)
                        {
                            shootableWeapon.getAmmo()[ammo_inc].Draw(gameTime, spriteBatch);
                        }
                    }
                }
            }
            foreach (MovableObject movableObject in selectedLevel.movableObjects)
            {
                if (movableObject is Enemy)
                {
                    Enemy enemy = (Enemy)movableObject;
                    ThrowAttack attack = ((ThrowAttack)enemy.getAttack());
                    if (attack != null && attack.getThrowObject() != null)
                    {
                        attack.getThrowObject().Walk(Direction.Right, attack.getThrowObject().speed);
                        attack.getThrowObject().Update(gameTime);
                        attack.getThrowObject().Draw(spriteBatch);
                    }
                }
            }
            updateScreenText(spriteBatch);
            try
            {
                spriteBatch.End();
            }
            catch (ObjectDisposedException e)
                {
                    Debug.Write(e);
                }
        }
        base.Draw(gameTime);
    }

edit:

This is how I load the content dynamically (I give the folder as a string when using this function)

    public void Loadlistontent(string contentFolder)
    {
        DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "/" + contentFolder);
        if (!dir.Exists)
            throw new DirectoryNotFoundException();

        FileInfo[] files = dir.GetFiles("*.*");
        foreach (FileInfo file in files)
        {
            if (!textureLoader.GetTextures().ContainsKey(file.Name))
            {
                string key = Path.GetFileNameWithoutExtension(file.Name);


                Texture2D texture = Content.Load<Texture2D>(contentFolder + "/" + key);
                textureLoader.addTexture(file.Name, texture);
            }
        }
    }
Kraishan
  • 443
  • 5
  • 14
  • 38

1 Answers1

0

At any point in your code, do you call a Content.Unload() or Texture2D.Dispose()? It seems like one of these might be hidden somewhere between Spritebatch.Begin() and Spritebatch.End(). It could be in updateScreenText(), as Joe mentioned.

The reason the exception is thrown at Spritebatch.End() is because spritebatch 'batches' your sprites together to make a single draw call. But it is at that single draw call that all exceptions of disposed content will be thrown, because that is when they're actually used.

Peethor
  • 151
  • 5
  • I don't use the unload() or dispose() anywhere. I checked the entire project twice now for that. The updateSCreenText simply writes some text on the screen with information that exists. – Kraishan Jan 14 '15 at 09:14
  • What I would do is comment everything between spritebatch.begin() and spritebatch.end(). If the code doesn't throw an exception then, just slowly keep uncommenting lines until you get the exception again. This tells you which line is the problem. If it's a method call, please post the method's code so I can take a look at it. – Peethor Jan 14 '15 at 10:01
  • But I use two spritebatch.begin's. Which one should be the troubling one? Seeing the startMenu.Draw is only executed if the startMenu.showMenu is true. Which is not the case in the other spritebatch.begin's situation. – Kraishan Jan 14 '15 at 12:16
  • I did notice my texture list (the list where I store the textures) contains this error on the texture: "Could not evaluate expression" on random values. This explains perhaps why I get this error randomly while playing my game. What triggers this error? I can reload the expression. I editted my main post with the way I load the content. – Kraishan Jan 14 '15 at 12:43
  • Sorry for the late reply. Obiously, as you stated, the troubling one is the second spritebatch.begin spritebatch.end pairs. So comment everything out between those lines, see if it works, and then line by line, uncomment them. – Peethor Jan 15 '15 at 06:08
  • Oh by the way, I just realised you're using a forward slash in `DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "/" + contentFolder);`. I think that should be `DirectoryInfo dir = new DirectoryInfo(Content.RootDirectory + "\" + contentFolder);`. Not sure if that would give an error like this, but I just noticed that in my own code, they're all "\" – Peethor Jan 15 '15 at 09:06
  • I found out the problem! I used ApplyChanges() for my graphicsdevicemanager in an update method. When I got rid of these it worked! – Kraishan Jan 15 '15 at 11:21