Skip to content

[BUG] Memory leak when create SKTypeface from file name or stream. #996

Closed

Description

@AlexNosk

Description

If create SKTypeface from file name or from stream, memory is not released. In the code example, there are commented lines, uncomment them to test creating typeface from family name (no problems with this one), from file name and memory stream. Attached screenshots shows that in the last two cases memory usage raise constantly.

Code

        for (int i = 0; i < 1000; i++)
        {
            using (SkiaSharp.SKBitmap bmp = new SkiaSharp.SKBitmap(100, 100))
            using (SkiaSharp.SKCanvas canvas = new SkiaSharp.SKCanvas(bmp))
            using (SkiaSharp.SKPaint paint = new SkiaSharp.SKPaint())
            //using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromFamilyName("Calibri")) // If create Typeface from family there is no memory leak.
            using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromFile("C:\\WINDOWS\\Fonts\\calibri.ttf")) // If create Typeface from file name
            //using (MemoryStream ms = new MemoryStream(File.ReadAllBytes("C:\\WINDOWS\\Fonts\\calibri.ttf"))) // Or stream, there is memory leak.
            //using (SkiaSharp.SKTypeface typeface = SkiaSharp.SKTypeface.FromStream(ms))
            {
                paint.Typeface = typeface;
                paint.TextSize = 12;

                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 10), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 20), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 30), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 40), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 50), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 60), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 70), paint);
                canvas.DrawText("Cool Text", new SkiaSharp.SKPoint(10, 80), paint);

                using (SkiaSharp.SKFileWStream fs = new SkiaSharp.SKFileWStream(@"C:\Temp\out.png"))
                {
                    SkiaSharp.SKPixmap.Encode(fs, bmp, SkiaSharp.SKEncodedImageFormat.Png, 100);
                }
            }

            GC.Collect();
        }

Basic Information

  • Version with issue: Tested and reproduced on the latest stable 1.68 and latest RC 1.68.1-rc.153
  • IDE: Visual Studio
  • Platform Target Frameworks: .NET Core 2.2

Screenshots
Memory usage when create SKTypeface from family name
family
Memory usage when create SkTypeface from file name
filename
Memory usage when create SkTypeface from memory stream
memorystream

Activity

  1. mattleibow commented on Nov 5, 2019

    @mattleibow
    Contributor

    I had a look and it appears to be some leak inside the native library when a stream is used. Even in the case of the filename, there is an underlying file stream that is opened.
    I'll need to investigate further to see why it is not being released.

    As a workaround, you can cache the typeface. This will use more memory than desired, but only once.

    // some "global" state
    var typefaces = new ConcurrentDictionary<string, SKTypeface>();
    ...
    
    // in loop
    var typeface = typefaces.GetOrAdd("C:\\WINDOWS\\Fonts\\calibri.ttf", key => SKTypeface.FromFile(key));

    Just make sure no not dispose the typefaces as you will then get a disposed item from the dictionary.

  2. AlexNosk commented on Nov 6, 2019

    @AlexNosk
    Author

    Thank you for the proposed workaround, I changed the code to use overload that creates SkTypeface from family name. But still the fix is required, because sometimes it is needed to create typeface from stream or file.

  3. mattleibow commented on Dec 22, 2019

    @mattleibow
    Contributor

    @AlexNosk Since the release of 1.68.1, I believe there were several major fixes for memory leaks. I am closing this issue as it appears to be fixed. If you fine that this is not the case, feel free to reopen this issue.

  4. ghost locked as resolved and limited conversation to collaborators on Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions