xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.62k stars 1.87k forks source link

[Bug] UWP Crash: ComCallHelpers.Call (__ComObject, RuntimeTypeHandle, Int32, Size) #12265

Open holecekp opened 4 years ago

holecekp commented 4 years ago

Description

AppCenter reports the following a crash of my application on UWP:

ComCallHelpers.Call (__ComObject, RuntimeTypeHandle, Int32, Size) System.Exception: Unspecified error (Exception from HRESULT: 0x80004005)

Stack Trace: uwpcrash26 9 2020 uwpcrash26 9 2020-2

I have a label which has FormattedString as its content. This formatted string can consists of a few parts that have different colors (however, in this case, the FormattedString had only a single Span). From the end of the stack trace: the view model updated a property that has type of FormattedString and that is data-bound to a Label. The view model then executed PropertyChanged to notify about this change. After that, LabelRenderer in Xamarin.Forms crashed the application.

Unfortunately, this will be one of many Xamarin.Forms bugs on UWP that either happen randomly, or are connected only to some computer configurations. I do excessive testing and I have never encountered this crash on my computer. Providing a repro solution will be almost impossible. Still, I think that even these bugs, which are very hard to reproduce, should not be ignored if they lead to crashes. I hope that the stack trace from AppCenter will be helpful.

Steps to Reproduce

  1. Unable to reproduce locally. The crash probably occurs randomly, or only on some device configurations. I have looked to Microsoft Partner Center Health reports and 3 crashes are reported at that exact time. So I guess this has been the same user and the app crashed repeatedly.

Expected Behavior

No crashes.

Actual Behavior

Application crashes.

Basic Information

Workaround

No workaround

holecekp commented 4 years ago

Microsoft Partner Center reports 6 hits in the last 72 hours in its Health Reporrts :-(. I have not been able to reproduce this bug on computer with Visual Studio, but I have reproduced it on a laptop. I have been using the app for about 2 minutes and, suddenly, it crashed.

hartez commented 4 years ago

This formatted string can consists of a few parts that have different colors (however, in this case, the FormattedString had only a single Span).

@holecekp Are you able to tell us anything more specific about the values in the formatted string? If we knew more about the content (including the span and the colors), that might help us track this down.

hartez commented 4 years ago

The method with the crash, FindDefaultLineHeight, is very weird. The UpdateText method in the renderer is using it to measure all of the heights of the spans, but it's doing so by iterating over the spans and, for each one,

  1. Converting it to a run
  2. Adding it to the renderer's TextBlock
  3. Measuring the TextBlock
  4. Removing it from the renderer's TextBlock
  5. Returning the height

After which the renderer immediately adds the run back to the TextBlock.

This seems really awkward and inefficient, unless I'm missing something. I'd need to dive deeper, but it sort of looks like RecalculateSpanPositions (which uses the list of heights gathered from this method) will size middle lines incorrectly if later spans are shorter than earlier spans.

holecekp commented 4 years ago

@hartez The application is a Morse code translator. The text has generally multiple lines. The content is provided by the user. It can be either a normal text, or a Morse code (characters ".", "-", "/", spaces and newlines).

The application can transmit the entered text as a Morse code using the speakers. The purpose of the formatted text is to show the current position what has already been transmitted:

The last possible situation is when the user did not enter any text yet (or when it was cleared) - a FormattedText without any spans is set.

I have looked at the stack trace and the crash happened in a function that cannot be called when the transmission is on. So when it crashed, there must have been only a single span with the whole text in the FormattedString, or a FormattedString without any spans.

Update:

jsuarezruiz commented 4 years ago

I have tried to reproduce the issue notifying the change of TextColor etc. every short time. But I have not managed to reproduce the issue :( However, in FindDefaultLineHeight we basically do what E.Z has already commented, we add a line, Measure it to obtain the size, eliminated the line and return the height. Although I have not managed to reproduce, I think we can accept the issue and find a more efficient way to get the line size.

Hay do you think @hartez?

holecekp commented 4 years ago

I also suggest using the release mode and run the app in x64 mode when trying to reproduce it. My experience is that these two settings makes various bugs on UWP much more likely to appear in general.

holecekp commented 4 years ago

By the way, the bug is also in 5.0.0 pre 3. I have reproduced the issue also in that version accidentally while I was trying to reproduce another issue.

The application was in the foreground but I was not using the computer for about 3 minutes (it seems that this period of inactivity increases the chance for reproducing this issue). Then I quickly clicked several times on a button that updates an Editor text and an Label with FormattedText (single Span without color settings). The application crashed.