0

I have been running into the same issue for a while now where once the Player has collected an object from the game, it is removed but the game still thinks that it is there and throws:

An unhandled exception of type 'System.ComponentModel.Win32Exception' occurred in System.Drawing.dll

Additional information: The operation completed successfully

I have read a few other posts and they seem to be saying that you should use Dispose() to remove the graphics after they have been collected to free up some memory but I am not sure if this is the correct solution for me nor do I know how to do this.

The warning above occurs in my Object class (it specifically highlights the line public Obj(Vector2 pos), this can be seen below.

class Obj : Microsoft.Xna.Framework.Game
{
    public Vector2 position;
    public float rotation = 0.0f;
    public Texture2D spriteIndex;
    public string spriteName;
    public float speed = 0.0f;
    public float scale = 1.0f;
    public bool alive = true;
    public Rectangle area;
    public bool solid = false;

    public int score;


    public Obj(Vector2 pos)
    {
        position = pos;
    }

    private Obj()
    {

    }


    public virtual void Update()
    {
        if (!alive) return;

        UpdateArea();
        pushTo(speed, rotation);
    }



    public virtual void LoadContent(ContentManager content)
    {
        spriteIndex = content.Load<Texture2D>("sprites\\" + spriteName);
        area = new Rectangle((int)position.X - (spriteIndex.Width / 2), (int)position.Y - (spriteIndex.Height / 2), spriteIndex.Width, spriteIndex.Height);
    }


    public virtual void Draw(SpriteBatch spriteBatch)
    {
        if (!alive) return;

        Rectangle Size;
        Vector2 center = new Vector2(spriteIndex.Width / 2, spriteIndex.Height / 2);

        spriteBatch.Draw(spriteIndex, position, null, Color.White, MathHelper.ToRadians(rotation), center, scale, SpriteEffects.None, 0);
    }

    public bool Collision(Vector2 pos, Obj obj)
    {
        Rectangle newArea = new Rectangle(area.X, area.Y, area.Width, area.Height);
        newArea.X += (int)pos.X;
        newArea.Y += (int)pos.Y;

        foreach (Obj o in Items.objList)
        {
            if (o.GetType() == obj.GetType() && o.solid)
                if (o.area.Intersects(newArea))
                    return true;
        }
        return false;
    }

    public Obj Collision(Obj obj)
    {
        foreach (Obj o in Items.objList)
        {
            if (o.GetType() == obj.GetType())
                if (o.area.Intersects(area))
                    return o;
        }
        return new Obj();
    }

    public void UpdateArea()
    {
        area.X = (int)position.X - (spriteIndex.Width / 2);
        area.Y = (int)position.Y - (spriteIndex.Height / 2);
    }

    public T CheckCollisionAgainst<T>() where T : Obj
    {
        // If collision detected, returns the colliding object; otherwise null.
        return Items.objList
            .OfType<T>()
            .FirstOrDefault(o => o.area.Intersects(area));
    }

    public virtual void pushTo(float pix, float dir)
    {
        float newX = (float)Math.Cos(MathHelper.ToRadians(dir));
        float newY = (float)Math.Sin(MathHelper.ToRadians(dir));
        position.X += pix * (float)newX;
        position.Y += pix * (float)newY;
    }
}
Community
  • 1
  • 1
Kieren Wells
  • 65
  • 1
  • 6
  • This might be useful http://stackoverflow.com/questions/12954227/in-xna-what-is-the-best-method-to-dispose-of-textures-i-no-longer-need – Dave Jun 23 '15 at 12:09
  • @Dave It kind of helps but I am not sure how I should apply that to my game in order to remove the warning – Kieren Wells Jun 23 '15 at 13:05
  • are you calling `spritebatch.begin()` and `spritebatch.end()`? – Robin Dijkhof Jun 23 '15 at 18:20
  • @RobinDijkhof not within the Object class (shown above), should i be calling `spriteBatch.Begin();` before the `Draw(spriteIndex` and then `spriteBatch.End();` after? – Kieren Wells Jun 23 '15 at 19:50
  • Not necessarily at that position, but before you draw with the spritbatch call begin and when you re ready call end. You could call begin before you draw-loop(I assume you have multiple objects) and end after. – Robin Dijkhof Jun 23 '15 at 20:12
  • @RobinDijkhof I do have multiple objects, so where would be the best place to start an end in the object class? – Kieren Wells Jun 23 '15 at 20:26
  • I assume you call the draw methods with a for-loop. call begin before the loop and end after the loop. – Robin Dijkhof Jun 23 '15 at 21:00

1 Answers1

0

try something along the lines:

protected override void Dispose(bool disposing)
{
    spriteIndex.Dispose() 
    // any other object to dispose
    base.Dispose(disposing);
}