C# XNA未知内存泄漏导致游戏崩溃

C# XNA未知内存泄漏导致游戏崩溃,c#,debugging,memory-leaks,xna,game-physics,C#,Debugging,Memory Leaks,Xna,Game Physics,你好 我正在XNA做一个太空射击游戏。游戏正常运行,我在代码中找不到任何错误。然而,当我从我的宇宙飞船发射了太多子弹时,游戏就崩溃了 我确实会在子弹离开屏幕时删除它们,但它们仍然会导致游戏崩溃 我收到的错误消息是: “System.Drawing.dll中发生类型为'System.ComponentModel.Win32Exception'的未处理异常” “其他信息:进程已结束” 标记行是: public Bullet(Texture2D texture, Color color, float

你好
我正在XNA做一个太空射击游戏。游戏正常运行,我在代码中找不到任何错误。然而,当我从我的宇宙飞船发射了太多子弹时,游戏就崩溃了

我确实会在子弹离开屏幕时删除它们,但它们仍然会导致游戏崩溃

我收到的错误消息是:

“System.Drawing.dll中发生类型为'System.ComponentModel.Win32Exception'的未处理异常” “其他信息:进程已结束”

标记行是:

public Bullet(Texture2D texture, Color color, float speed, int scale)  
以下是项目符号的相关代码:

Game1.cs

   // This is a member variable at the top of my program
   public List<Bullet> bullets = new List<Bullet>();  

   // Further down
    public void UpdateBullets()
    {
        foreach (Bullet bullet in bullets)
        {
            bullet.position.Y += bullet.speed;
            if (bullet.position.Y >= GraphicsDevice.Viewport.Height || bullet.position.Y + bullet.height <= 0
            || bullet.position.X < 0 || bullet.position.X - bullet.width > GraphicsDevice.Viewport.Width)
                bullet.isVisible = false;
        }
        for (int i = 0; i < bullets.Count(); i++)
        {
            if (!bullets[i].isVisible)
            {
                bullets[i].texture = null;
                bullets[i] = null;
                bullets.Remove(bullets[i]);
            }
        }
    }  

    public void Shoot(Entity e, Texture2D texture)
    {
        Bullet newBullet = new Bullet(texture, e.color, e.bulletSpeed, 16);
        newBullet.owner = e;
        newBullet.SetPositionByBottom(e.bulletPoint);
        newBullet.isVisible = true;

        if (bullets.Count() < maxBullets)
        {
            bullets.Add(newBullet);
        }
    }  

        public void DrawBullets()
    {
        foreach (Bullet bullet in bullets)
        {
            bullet.Draw(spriteBatch);
        }
    }  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace SpaceShooter
{
    class Bullet : GameObject
    {
        public float speed;
        public bool isVisible;
        public Entity owner;

        public Bullet(Texture2D texture, Color color, float speed, int scale)
        {
            this.color = color;
            this.texture = texture;
            this.speed = speed;
            width = scale;
            height = scale * 2;
            isVisible = false;
        }
    }
}  

关于内存泄漏是从哪里来的,或者它是否是内存泄漏,有什么想法吗?

试着向后遍历项目符号列表:

for (int i = bullets.Count()-1; i >= 0; i--)
{
    if (!bullets[i].isVisible)
    {
        bullets[i].texture = null;
        bullets[i] = null;
        bullets.Remove(bullets[i]);
    }
}

当您在i处删除列表中的一个项目时,i+1处的项目将被下推到i,但您现在跳过了该项目。我认为这可能是导致你内存泄漏的原因。向后工作应该可以避免这个问题。

这听起来很合乎逻辑,但遗憾的是,它不起作用。我仍然会撞车,同样的线路也会被标记。还有其他想法吗?或者更简单的方法:
bullets.removeall(item=>!item.visible)
public Bullet(Texture2D纹理、颜色、浮点速度、整数比例)听起来不像内存泄漏。你能通过任务管理器确认吗?我同意下面骷髅的建议,改变你移除物品的方式。即使如此,如果不这样做,通常会导致不同类型的异常,例如“collection was changed”。老实说,我不知道如何通过task Manager进行检查。另外,我也尝试了骷髅提供的方法,但没有效果。当你运行游戏时,在任务管理器中记录它消耗了多少内存。作为一个粗略的指导-一个32位的.NET应用程序如果不做任何“太特殊”的内存操作,将在大约1.5GB的内存使用率下“爆炸”(确切的细节将不在本评论中)。检查泄漏的更准确方法是通过VisualStudio的代码分析