Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA-保存帧时内存泄漏_C#_Memory Leaks_Xna - Fatal编程技术网

C# XNA-保存帧时内存泄漏

C# XNA-保存帧时内存泄漏,c#,memory-leaks,xna,C#,Memory Leaks,Xna,我使用XNA游戏项目创建3D场景的帧。然而,我在使用MemoryStream时发生了内存泄漏。下面的代码作为Draw函数的一部分调用 byte[] FrameSave() { int w = GraphicsDevice.PresentationParameters.BackBufferWidth; int h = GraphicsDevice.PresentationParameters.BackBufferHeight; //

我使用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.>。>

不知道这是否有帮助,但请尝试对
内存流使用
using
语句。(
使用(MemoryStream ms=new MemoryStream())
)我尝试过,但效果与以前一样。可能是因为您在内存流之后处理图片吗?即使我在保存后立即处理图片,它仍然会导致泄漏……一个想法。MonoGame是否实现了这些功能,如果是的话,您是否可以使用开源项目中的那些例程(当然有正确的属性)。您也可以将目标锁定在mono游戏而不是XNA,从而获得跨平台的好处。