【问题标题】:Memory leak in XNA gameXNA 游戏中的内存泄漏
【发布时间】:2013-02-21 22:48:03
【问题描述】:

我有一个游戏的一小部分,它基本上显示了一本书的一页。 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);
    }

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

【问题讨论】:

  • 您在使用代码分析吗? CA2000 规则将帮助指出它看不到已处理的一次性对象的实例。

标签: c# windows-phone-7 memory-leaks xna


【解决方案1】:

你的漏洞在这里:

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

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

【讨论】:

  • 所以我应该在每次抽奖后立即调用它的 Dispose() 吗?我不明白电话应该在哪里。另外,我在一般开发时从未调用过 Dispose() 。我假设 ContentManager.Unload() 会自动执行此操作,对吗?
  • 可以在每一帧上新建一个 Texture2D 和 Dispose() 它,但这对性能来说很糟糕。相反,创建一个 Texture2D 一次(在初始化时),然后在每一帧上重新使用它。当您知道不再使用它时调用 Dispose()。 ContentManager 自动处理它管理的对象,即您通过 ContentManager.Load 获得的对象。当您像在这里一样自己新建对象时,您有责任管理它们的生命周期。
猜你喜欢
  • 2016-03-09
  • 1970-01-01
  • 2013-01-15
  • 1970-01-01
  • 2018-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多