From c270bbe8bdcf5263caca5108536b965ef79e6d4c Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Wed, 25 Jan 2023 19:04:22 +0100 Subject: [PATCH] Fix memory leak with GPUBuffer --- Capy64/Core/Drawing.cs | 29 ++++++++++++-------------- Capy64/LuaRuntime/Libraries/GPU.cs | 4 ++-- Capy64/LuaRuntime/Objects/GPUBuffer.cs | 8 ++++--- 3 files changed, 20 insertions(+), 21 deletions(-) diff --git a/Capy64/Core/Drawing.cs b/Capy64/Core/Drawing.cs index e053a6f..617471f 100644 --- a/Capy64/Core/Drawing.cs +++ b/Capy64/Core/Drawing.cs @@ -28,17 +28,13 @@ public class Drawing : IDisposable _spriteBatch = new SpriteBatch(_canvas.GraphicsDevice); _graphicsDevice = _canvas.GraphicsDevice; - Grid = new Color[_canvas.Width * _canvas.Height]; - _canvas.GetData(Grid); - _whitePixel = new Texture2D(_spriteBatch.GraphicsDevice, 1, 1, mipmap: false, SurfaceFormat.Color); _whitePixel.SetData(new Color[1] { Color.White }); if (isDrawing) Begin(); } } - public Color[] Grid; - + public Drawing() { _fontSystem = new FontSystem(); @@ -64,7 +60,6 @@ public class Drawing : IDisposable _spriteBatch.End(); _graphicsDevice.SetRenderTarget(null); _isDrawing = false; - _canvas.GetData(Grid); } public void DrawString(Vector2 pos, string text, Color color, int size = 13) @@ -81,27 +76,28 @@ public class Drawing : IDisposable public void Plot(Point point, Color color) { - //var grid = new Color[_canvas.Width * _canvas.Height]; - _canvas.GetData(Grid); - if (point.X < 0 || point.Y < 0) return; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return; - Grid[point.X + (point.Y * _canvas.Width)] = color; + + var grid = new Color[_canvas.Width * _canvas.Height]; + _canvas.GetData(grid); + + grid[point.X + (point.Y * _canvas.Width)] = color; - _canvas.SetData(Grid); + _canvas.SetData(grid); } public void Plot(IEnumerable points, Color color) { - //var grid = new Color[_canvas.Width * _canvas.Height]; - _canvas.GetData(Grid); + var grid = new Color[_canvas.Width * _canvas.Height]; + _canvas.GetData(grid); foreach (var point in points) { if (point.X < 0 || point.Y < 0) continue; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) continue; - Grid[point.X + (point.Y * _canvas.Width)] = color; + grid[point.X + (point.Y * _canvas.Width)] = color; } - _canvas.SetData(Grid); + _canvas.SetData(grid); } public Color GetPixel(Point point) @@ -109,7 +105,8 @@ public class Drawing : IDisposable if (point.X < 0 || point.Y < 0) return Color.Black; if (point.X >= _canvas.Width || point.Y >= _canvas.Height) return Color.Black; - return Grid[point.X + (point.Y * _canvas.Width)]; + var grid = new Color[_canvas.Width * _canvas.Height]; + return grid[point.X + (point.Y * _canvas.Width)]; } public void UnsafePlot(Point point, Color color) diff --git a/Capy64/LuaRuntime/Libraries/GPU.cs b/Capy64/LuaRuntime/Libraries/GPU.cs index be97a06..b8953de 100644 --- a/Capy64/LuaRuntime/Libraries/GPU.cs +++ b/Capy64/LuaRuntime/Libraries/GPU.cs @@ -385,7 +385,7 @@ public class GPU : IPlugin var buffer = new uint[_game.Width * _game.Height]; _game.Drawing.Canvas.GetData(buffer); - var handle = new GPUBuffer(_game, buffer); + var handle = new GPUBuffer(buffer); handle.Push(L); @@ -413,7 +413,7 @@ public class GPU : IPlugin var buffer = new uint[_game.Width * _game.Height]; - var handle = new GPUBuffer(_game, buffer); + var handle = new GPUBuffer(buffer); handle.Push(L); diff --git a/Capy64/LuaRuntime/Objects/GPUBuffer.cs b/Capy64/LuaRuntime/Objects/GPUBuffer.cs index c1bd6cf..dea6889 100644 --- a/Capy64/LuaRuntime/Objects/GPUBuffer.cs +++ b/Capy64/LuaRuntime/Objects/GPUBuffer.cs @@ -7,11 +7,9 @@ public class GPUBuffer { public const string ObjectType = "GPUBuffer"; - private static IGame _game; private uint[] _buffer; - public GPUBuffer(IGame game, uint[] buffer) + public GPUBuffer(uint[] buffer) { - _game = game; _buffer = buffer; } @@ -34,6 +32,10 @@ public class GPUBuffer L.PushString("__gc"); L.PushCFunction(LM_GC); L.SetTable(-3); + + L.PushString("__close"); + L.PushCFunction(LM_GC); + L.SetTable(-3); } L.PushObject(_buffer);