Skip to content

Commit

Permalink
Try to fix memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
timia2109 committed Mar 19, 2024
1 parent ad238f7 commit 0529b99
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Layouting/BorderElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
5 changes: 4 additions & 1 deletion Layouting/Element.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ SKPoint StartPoint
/// <summary>
/// Abstract base Element. Elements are designed to be used only one time!
/// </summary>
public abstract class Element
public abstract class Element : IDisposable
{
/// <summary>
/// Caching calculated size
Expand Down Expand Up @@ -45,4 +45,7 @@ public SKSize GetSize(DrawContext drawContext)
/// </summary>
/// <param name="drawContext">Context</param>
public abstract void Draw(DrawContext drawContext);

public virtual void Dispose()
{ }
}
9 changes: 9 additions & 0 deletions Layouting/ElementCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,13 @@ protected record struct ChildrenSizes
};
}
}

public override void Dispose()
{
base.Dispose();
foreach (var child in Children)
{
child.Dispose();
}
}
}
6 changes: 6 additions & 0 deletions Layouting/TextElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
4 changes: 2 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");

})
Expand Down
2 changes: 1 addition & 1 deletion Template/ScribanScreenProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task<SKBitmap> 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,
Expand Down

0 comments on commit 0529b99

Please sign in to comment.