Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak in VerticalMetricsTable #409

Merged
merged 1 commit into from
Aug 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/SixLabors.Fonts/BigEndianBinaryReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace SixLabors.Fonts;
/// BinaryReader using big-endian encoding.
/// </summary>
[DebuggerDisplay("Start: {StartOfStream}, Position: {BaseStream.Position}")]
internal class BigEndianBinaryReader : IDisposable
internal sealed class BigEndianBinaryReader : IDisposable
{
/// <summary>
/// Buffer used for temporary storage before conversion into primitives
Expand Down
5 changes: 4 additions & 1 deletion src/SixLabors.Fonts/Tables/General/VerticalMetricsTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ internal short GetTopSideBearing(int glyphIndex)
return null;
}

return Load(binaryReader, headTable.NumberOfVMetrics, profileTable.GlyphCount);
using (binaryReader)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice job finding this! Did you find it profiling or just by chance?

Copy link
Contributor Author

@Bykiev Bykiev Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've used Roslyn analyzer, there are some other issues, but due to used patterns in Fonts/ImageSharp I still investigating if it can be fixed (there are some non-sealed classes with non-virtual Dispose and some classes has a local variables, where class is not an owner of it). For. ex., FontReader class has a variable stream, but it has no Dispose, but some other classes implements it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be careful with that analyzer, I've seen many, many false positives.

FontReader doesn't own the input stream so shouldn't be responsible for its lifecycle. It's perfectly fine for the type to use a private variable to hold the reference.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it'll should be used carefully, because of such FPs

Copy link
Contributor Author

@Bykiev Bykiev Aug 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll prepare some interesting cases and will add an appropriate discussion if needed, still need some time to research all warnings...

{
return Load(binaryReader, headTable.NumberOfVMetrics, profileTable.GlyphCount);
}
}

public static VerticalMetricsTable Load(BigEndianBinaryReader reader, int metricCount, int glyphCount)
Expand Down
Loading