XNA游戏内存泄漏

本文关键字:泄漏 内存 游戏 XNA | 更新日期: 2023-09-27 18:00:32

我有一个游戏的小部分,它基本上显示了一本书的一页。Update方法不起任何作用,Draw方法如下:

public override void Draw(GameTime gameTime)
{
    SpriteBatch spriteBatch = ScreenManager.SpriteBatch;
    spriteBatch.Begin();
    spriteBatch.Draw(background.Image, background.Bounds, Color.White);
    spriteBatch.DrawString(font, name, new Vector2(ScreenManager.GraphicsDevice.Viewport.Width / 2 - font.MeasureString(name).X / 2, 100), Color.Black);
    spriteBatch.DrawString(font, currentPage.PageText, new Vector2(currentPage.TextArea.X, currentPage.TextArea.Y), Color.Black);
    if (currentPageNumber != 1)
    {
        spriteBatch.Draw(next.Image, previous.Bounds, null, Color.White, 0.0F, Vector2.Zero, SpriteEffects.FlipHorizontally, 0.0F);
    }
    if (currentPageNumber != noOfPages)
    {
        spriteBatch.Draw(next.Image, next.Bounds, Color.White);
    }
    DrawEmptyRectangle(spriteBatch, 3F, Color.Black, new Rectangle(back.Bounds.X - 1, back.Bounds.Y - 1, back.Bounds.Width + 1, back.Bounds.Height + 1));
    DrawEmptyRectangle(spriteBatch, 3F, Color.Black, new Rectangle(play.Bounds.X - 1, play.Bounds.Y - 1, play.Bounds.Width + 1, play.Bounds.Height + 1));
    DrawCenteredText(spriteBatch, font, BACK_TEXT, back.Bounds, Color.Black);
    DrawCenteredText(spriteBatch, font, PLAY_TEXT, play.Bounds, Color.Black);
    spriteBatch.End();
}
public void DrawCenteredText(SpriteBatch spriteBatch, SpriteFont font, string text, Rectangle toCenterIn, Color textColor)
{
    spriteBatch.DrawString(font, text, new Vector2(toCenterIn.X + toCenterIn.Width / 2 - font.MeasureString(text).X / 2, toCenterIn.Y + toCenterIn.Height / 2 - font.MeasureString(text).Y / 2), textColor);
}
public void DrawEmptyRectangle(SpriteBatch spriteBatch, float width, Color color, Rectangle rectangle)
{
    Vector2 topLeft = new Vector2(rectangle.Left, rectangle.Top);
    Vector2 bottomLeft = new Vector2(rectangle.Left, rectangle.Bottom);
    Vector2 topRight = new Vector2(rectangle.Right, rectangle.Top);
    Vector2 bottomRight = new Vector2(rectangle.Right, rectangle.Bottom);
    _2DLine.Draw(ScreenManager.GraphicsDevice, spriteBatch, width, color, topLeft, topRight);
    _2DLine.Draw(ScreenManager.GraphicsDevice, spriteBatch, width, color, bottomLeft, bottomRight);
    _2DLine.Draw(ScreenManager.GraphicsDevice, spriteBatch, width, color, topLeft, new Vector2(bottomLeft.X, bottomLeft.Y + width));
    _2DLine.Draw(ScreenManager.GraphicsDevice, spriteBatch, width, color, topRight, bottomRight);
}

这是_2DLine.Draw方法:

public static void Draw(GraphicsDevice GraphicsDevice, SpriteBatch batch, float width, Color color, Vector2 point1, Vector2 point2)
{
    Texture2D blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
    blank.SetData(new[] { Color.White });
    float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
    float length = Vector2.Distance(point1, point2);
    batch.Draw(blank, point1, null, color, angle, Vector2.Zero, new Vector2(length, width), SpriteEffects.None, 0);
    }

问题是,我让程序运行的时间越长,它分配的内存就越多。

XNA游戏内存泄漏

您的泄漏在这里:

Texture2D blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);

你不应该像这样为每一帧分配纹理。创建一个并重用它。完成后,调用它的Dispose()方法,以便它可以释放其底层Direct3D资源。