C# XNA游戏中的内存泄漏
我有一个游戏的一小部分,它基本上显示了一本书中的一页。Update方法不执行任何操作,Draw方法如下所示:C# XNA游戏中的内存泄漏,c#,windows-phone-7,memory-leaks,xna,C#,Windows Phone 7,Memory Leaks,Xna,我有一个游戏的一小部分,它基本上显示了一本书中的一页。Update方法不执行任何操作,Draw方法如下所示: public override void Draw(GameTime gameTime) { SpriteBatch spriteBatch = ScreenManager.SpriteBatch; spriteBatch.Begin(); spriteBatch.Draw(background.Image, background.Bounds, Color.W
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);
}
问题是,我让程序运行的时间越长,它分配的内存就越多。您的漏洞在这里:
Texture2D blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
您不应该像这样为每个帧分配纹理。创建一个并重新使用它。完成后,调用其Dispose()方法,以便释放其底层Direct3D资源。您的漏洞如下:
Texture2D blank = new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
您不应该像这样为每个帧分配纹理。创建一个并重新使用它。完成后,调用它的Dispose()方法,这样它就可以释放其底层Direct3D资源。是否使用代码分析?CA2000规则将有助于指出它无法看到的一次性对象的实例已被处置。是否使用代码分析?CA2000规则将有助于指出它无法看到的一次性对象的实例已被处置。因此,我应该在每次绘制之后调用它的Dispose()?我不知道电话应该在哪里。另外,在一般开发时,我从未调用过Dispose()。我假设ContentManager.Unload()会自动执行此操作,对吗?您可以新建一个Texture2D并在每个帧上进行处理(),但这对性能来说是非常糟糕的。相反,创建一次Texture2D(在初始化时),并在每一帧上重复使用它。当您知道不再使用Dispose()绘图时,调用Dispose()。ContentManager自动处理其管理的对象,即通过ContentManager.Load获得的对象。当你像这里这样自己创建对象时,你负责管理它们的生命周期。所以我应该在每次绘制之后调用它的Dispose()?我不知道电话应该在哪里。另外,在一般开发时,我从未调用过Dispose()。我假设ContentManager.Unload()会自动执行此操作,对吗?您可以新建一个Texture2D并在每个帧上进行处理(),但这对性能来说是非常糟糕的。相反,创建一次Texture2D(在初始化时),并在每一帧上重复使用它。当您知道不再使用Dispose()绘图时,调用Dispose()。ContentManager自动处理其管理的对象,即通过ContentManager.Load获得的对象。当你像在这里一样自己创建新对象时,你有责任管理它们的生命周期。