Fix memory leak with GPUBuffer

This commit is contained in:
Alessandro Proto 2023-01-25 19:04:22 +01:00
parent 024b1f6a19
commit c270bbe8bd
3 changed files with 20 additions and 21 deletions

View file

@ -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<Point> 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)

View file

@ -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);

View file

@ -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);