From 0529b990c89663e1cee65103be69d518b26a166a Mon Sep 17 00:00:00 2001 From: Tim Ittermann Date: Tue, 19 Mar 2024 16:07:20 +0100 Subject: [PATCH] Try to fix memory leaks --- Layouting/BorderElement.cs | 2 +- Layouting/Element.cs | 5 ++++- Layouting/ElementCollection.cs | 9 +++++++++ Layouting/TextElement.cs | 6 ++++++ Program.cs | 4 ++-- Template/ScribanScreenProvider.cs | 2 +- 6 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Layouting/BorderElement.cs b/Layouting/BorderElement.cs index cc20bbe..612c821 100644 --- a/Layouting/BorderElement.cs +++ b/Layouting/BorderElement.cs @@ -49,7 +49,7 @@ private void DrawLine(DrawContext drawContext, SKPoint startPoint, { if (width == 0) return; - var paint = new SKPaint + using var paint = new SKPaint { Color = SKColors.Black, StrokeWidth = width, diff --git a/Layouting/Element.cs b/Layouting/Element.cs index bc530aa..fdd26cb 100644 --- a/Layouting/Element.cs +++ b/Layouting/Element.cs @@ -13,7 +13,7 @@ SKPoint StartPoint /// /// Abstract base Element. Elements are designed to be used only one time! /// -public abstract class Element +public abstract class Element : IDisposable { /// /// Caching calculated size @@ -45,4 +45,7 @@ public SKSize GetSize(DrawContext drawContext) /// /// Context public abstract void Draw(DrawContext drawContext); + + public virtual void Dispose() + { } } diff --git a/Layouting/ElementCollection.cs b/Layouting/ElementCollection.cs index e3007b0..4d8d1a6 100644 --- a/Layouting/ElementCollection.cs +++ b/Layouting/ElementCollection.cs @@ -49,4 +49,13 @@ protected record struct ChildrenSizes }; } } + + public override void Dispose() + { + base.Dispose(); + foreach (var child in Children) + { + child.Dispose(); + } + } } diff --git a/Layouting/TextElement.cs b/Layouting/TextElement.cs index 0f06ff2..cf99bc5 100644 --- a/Layouting/TextElement.cs +++ b/Layouting/TextElement.cs @@ -22,4 +22,10 @@ protected override SKSize CalculateSize(DrawContext drawContext) var width = paint.MeasureText(content); return new SKSize(width, paint.TextSize); } + + public override void Dispose() + { + base.Dispose(); + paint.Dispose(); + } } diff --git a/Program.cs b/Program.cs index 52b7ff9..20f3298 100644 --- a/Program.cs +++ b/Program.cs @@ -28,8 +28,8 @@ app.MapGet("/preview/{providerId}", async (string providerId, ScreenRepository repo) => { - var image = await repo.GetImageAsync(providerId); - var data = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100); + using var image = await repo.GetImageAsync(providerId); + using var data = image.Encode(SkiaSharp.SKEncodedImageFormat.Png, 100); return Results.File(data.ToArray(), "image/png"); }) diff --git a/Template/ScribanScreenProvider.cs b/Template/ScribanScreenProvider.cs index ff018f1..e0bcc86 100644 --- a/Template/ScribanScreenProvider.cs +++ b/Template/ScribanScreenProvider.cs @@ -16,7 +16,7 @@ public async Task GetImageAsync() { var fileContent = await File.ReadAllTextAsync(path); using var xml = await renderer.RenderToStreamAsync(fileContent); - var element = layoutDeserializer.DeserializeXml(xml); + using var element = layoutDeserializer.DeserializeXml(xml); return DrawManager.Draw( Constants.EPaperDisplaySize,