保存帧时内存泄漏

本文关键字:泄漏 内存 保存 | 更新日期: 2023-09-27 18:14:10

我使用XNA游戏项目来创建我的3D场景框架。然而,我得到了内存泄漏,而使用MemoryStream。下面的代码是作为Draw函数的一部分调用的。

    byte[] FrameSave()
    {
        int w = GraphicsDevice.PresentationParameters.BackBufferWidth;
        int h = GraphicsDevice.PresentationParameters.BackBufferHeight;
        //pull the picture from the buffer 
        int[] backBuffer = new int[w * h];
        GraphicsDevice.GetBackBufferData(backBuffer);
        //copy into a texture 
        Texture2D texture = new Texture2D(GraphicsDevice, w, h, false, GraphicsDevice.PresentationParameters.BackBufferFormat);
        texture.SetData(backBuffer);
        MemoryStream ms = new MemoryStream();
        texture.SaveAsJpeg(ms, w, h); //MEMORYLEAK
        byte[] zframe = ms.ToArray();
        ms.Close();
        ms.Dispose();
        texture.Dispose();            
        return zframe;
    }

任何帮助将不胜感激。

保存帧时内存泄漏

啊,我在其他线程中发现了响应:

根据这个Texture2D。SaveAsJpeg(以及Texture2D.SaveAsPng)有内存泄漏。解决方案是(不幸的是)创建自己的纹理保存例程。

谢谢XNA。>>。