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 ForegroundTextTransformation objects mem leak #373

Merged
merged 2 commits into from
Dec 1, 2023
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
15 changes: 3 additions & 12 deletions src/AvaloniaEdit.TextMate/TextMateColoringTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ namespace AvaloniaEdit.TextMate
{
public class TextMateColoringTransformer :
GenericLineTransformer,
IModelTokensChangedListener,
ForegroundTextTransformation.IColorMap
IModelTokensChangedListener
{
private Theme _theme;
private IGrammar _grammar;
Expand Down Expand Up @@ -77,6 +76,7 @@ private void TextView_VisualLinesChanged(object sender, EventArgs e)
public void Dispose()
{
_textView.VisualLinesChanged -= TextView_VisualLinesChanged;
_brushes.Clear();
}

public void SetTheme(Theme theme)
Expand Down Expand Up @@ -105,15 +105,6 @@ public void SetGrammar(IGrammar grammar)
}
}

IBrush ForegroundTextTransformation.IColorMap.GetBrush(int colorId)
{
if (_brushes == null)
return null;

_brushes.TryGetValue(colorId, out IBrush result);
return result;
}

protected override void TransformLine(DocumentLine line, ITextRunConstructionContext context)
{
try
Expand Down Expand Up @@ -190,7 +181,7 @@ private void GetLineTransformations(int lineNumber, List<TMToken> tokens, Foregr
if (transformations[i] == null)
transformations[i] = new ForegroundTextTransformation();

transformations[i].ColorMap = this;
transformations[i].ColorMap = _brushes;
transformations[i].ExceptionHandler = _exceptionHandler;
transformations[i].StartOffset = lineOffset + startIndex;
transformations[i].EndOffset = lineOffset + endIndex;
Expand Down
29 changes: 17 additions & 12 deletions src/AvaloniaEdit.TextMate/TextTransformation.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;

using AvaloniaEdit.Document;

Expand All @@ -13,12 +14,7 @@ public abstract class TextTransformation : TextSegment

public class ForegroundTextTransformation : TextTransformation
{
public interface IColorMap
{
AM.IBrush GetBrush(int color);
}

public IColorMap ColorMap { get; set; }
public Dictionary<int, AM.IBrush> ColorMap { get; set; }
public Action<Exception> ExceptionHandler { get; set; }
public int ForegroundColor { get; set; }
public int BackgroundColor { get; set; }
Expand Down Expand Up @@ -47,13 +43,13 @@ public override void Transform(GenericLineTransformer transformer, DocumentLine
}

transformer.SetTextStyle(line, formattedOffset, endOffset - line.Offset - formattedOffset,
ColorMap.GetBrush(ForegroundColor),
ColorMap.GetBrush(BackgroundColor),
GetFontStyle(),
GetFontWeight(),
IsUnderline());
GetBrush(ForegroundColor),
GetBrush(BackgroundColor),
GetFontStyle(),
GetFontWeight(),
IsUnderline());
}
catch(Exception ex)
catch (Exception ex)
{
ExceptionHandler?.Invoke(ex);
}
Expand Down Expand Up @@ -85,5 +81,14 @@ bool IsUnderline()

return false;
}

AM.IBrush GetBrush(int colorId)
{
if (ColorMap == null)
return null;

ColorMap.TryGetValue(colorId, out AM.IBrush result);
return result;
}
}
}
Loading