Skip to content

[BUG] SKShader doesn't release all memory on Dispose() #3378

@michaldobrodenka

Description

@michaldobrodenka

Description

I think the bug is in ToHarfBuzzBlob() extension - it doesn't dispose SKStreamAsset in all paths.

Code

It seems that this code leaks SKStreamAssets even if properly disposed:

    {
        public SKPaint Paint { get; set; }
        public SKShaper Shaper { get; set; }

        private bool disposedValue;

        public TextSegmentShaper(SKTypeface typeface, SKPaint fromPaint)
        {
            Paint = new SKPaint()
            {
                Style = fromPaint.Style,
                IsAntialias = fromPaint.IsAntialias,
                Color = fromPaint.Color,
                Typeface = typeface,
                TextSize = fromPaint.TextSize,
                TextAlign = fromPaint.TextAlign,
                SubpixelText = fromPaint.SubpixelText,
            };

            Shaper = new SKShaper(typeface);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                Paint?.Dispose();
                Shaper?.Dispose();

                Paint = null;
                Shaper = null;

                disposedValue = true;
            }
        }```

### Expected Behavior

_No response_

### Actual Behavior

_No response_

### Version of SkiaSharp

3.116.0 (Current)

### Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

### IDE / Editor

Visual Studio (Windows)

### Platform / Operating System

Windows

### Platform / Operating System Version

First observed bug on Android, but seems to be a problem also on Windows.

### Devices

_No response_

### Relevant Screenshots

_No response_

### Relevant Log Output

```shell

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    New

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions