Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,15 @@ internal static void RecalculateSpanPositions(this TextBlock control, FormattedS
lineHeight = endRect.Bottom - endRect.Top;
}

// Guard against a zero (or negative) line height. This can happen when the
// TextBlock was measured while collapsed/hidden, which caches a
// MeasuredLineHeight of 0. Without this check, causing an infinite loop and an eventual
// OutOfMemoryException as lineHeights grows without bound.
if (lineHeight <= 0)
{
break;
}
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Resolved

lineHeights.Add(lineHeight);
yaxis += lineHeight;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using System.Threading.Tasks;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Handlers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
Expand Down Expand Up @@ -38,5 +40,44 @@ Task<bool> GetPlatformIsVisible(LabelHandler labelHandler)
return nativeView.Visibility == Microsoft.UI.Xaml.Visibility.Visible;
});
}

[Fact]
public async Task FormattedTextDoesNotHangOrCrashWhenLabelIsShownAfterBeingSetWhileHidden()
{
SetupBuilder();

var label = new Label
{
IsVisible = false,
WidthRequest = 30,
HorizontalOptions = LayoutOptions.Start,
LineBreakMode = LineBreakMode.WordWrap,
};

var layout = new VerticalStackLayout { label };

await AttachAndRun(layout, async (handler) =>
{
label.FormattedText = new FormattedString
{
Spans =
{
new Span { Text = "Aa Bb Cc" },
new Span { Text = "\n" },
}
};
Comment thread
SubhikshaSf4851 marked this conversation as resolved.
Resolved

await Task.Delay(50);

// Showing the label triggers a real arrange pass, which calls
// RecalculateSpanPositions(). Before the fix, this would hang the UI thread and
// eventually crash with an OutOfMemoryException because the label wraps to 3
// lines ("Aa", "Bb", "Cc") at this width. Simply completing (instead of hanging or
// throwing) proves the fix works.
label.IsVisible = true;

await Task.Delay(250);
});
}
}
}
Loading