xamarin / Xamarin.Forms

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

[Bug] iOS Material Renderers can cause NRE. #15669

Closed P3PPP closed 1 year ago

P3PPP commented 1 year ago

Description

In rare cases on iOS, Material Renderers' ApplyThemeIfNeeded can be called in a situation where Element == null, which causes a NullReferenceException.

I think it is necessary to improve the null check of Element.Background in all ApplyThemeIfNeeded of the iOS version Material Renderer.

// called from LayoutSubViews
protected virtual void ApplyThemeIfNeeded()
{
    // Element can be null.
    var bgBrush = Element.Background;

    if (Brush.IsNullOrEmpty(bgBrush))
        return;

    UpdateBackground();
}

Steps to Reproduce

This is a procedure that tends to cause problems in the reproduction code using the Material version Frame in ListView Items.

Expected Behavior

Exception does not occur.

Actual Behavior

NullReferenceException is thrown at ApplyThemeIfNeeded.

Basic Information

Environment

Show/Hide Visual Studio info ``` Visual Studio Professional 2022 for Mac Version 17.4 (build 2406) Installation UUID: 436218ec-a61a-4c9e-8829-f39ef2ec4618 Runtime .NET 6.0.9 (64-bit) Architecture: X64 Roslyn (Language Service) 4.4.0-3.22461.4+8ab250290a4010c11a21521f78dbc87dbb7aac81 NuGet Version: 6.3.1.1 .NET SDK (x64) SDK: /usr/local/share/dotnet/sdk/7.0.100/Sdks SDK Versions: 7.0.100 6.0.403 6.0.302 6.0.301 6.0.106 5.0.408 3.1.420 3.1.410 3.1.300 3.1.202 3.0.100 2.1.700 MSBuild SDKs: /Applications/Visual Studio.app/Contents/MonoBundle/MSBuild/Current/bin/Sdks .NET Runtime (x64) Runtime: /usr/local/share/dotnet/dotnet Runtime Versions: 7.0.0 6.0.11 6.0.7 6.0.6 5.0.17 3.1.26 3.1.16 3.0.0 2.1.23 Xamarin.Profiler Version: 1.8.0.19 Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler Updater Version: 11 Apple Developer Tools Xcode 14.0.1 (21336) Build 14A400 Xamarin.Mac Version: 8.12.0.2 (Visual Studio Professional) Hash: 87f98a75e Branch: d17-3 Build date: 2022-07-25 20:18:54-0400 Xamarin.iOS Version: 16.0.0.72 (Visual Studio Professional) Hash: 6756a1146 Branch: release/6.0.4xx-xcode14 Build date: 2022-09-21 08:51:06-0400 Xamarin Designer Version: 17.4.0.136 Hash: d49c9ff6d3 Branch: remotes/origin/d17-4 Build date: 2022-11-02 23:42:23 UTC Xamarin.Android Version: 13.1.0.1 (Visual Studio Professional) Commit: xamarin-android/d17-4/13ba222 Android SDK: /Users/naokitaguchi/Library/Developer/Xamarin/android-sdk-macosx Supported Android versions: 8.0 (API level 26) 6.0 (API level 23) 12.0 (API level 31) 8.1 (API level 27) 5.1 (API level 22) 5.0 (API level 21) 11.0 (API level 30) 10.0 (API level 29) 7.1 (API level 25) 9.0 (API level 28) 13.0 (API level 33) 7.0 (API level 24) SDK Command-line Tools Version: 7.0 SDK Platform Tools Version: 33.0.2 SDK Build Tools Version: 32.0.0 Build Information: Mono: a96bde9 Java.Interop: xamarin/java.interop/d17-4@fcc33ce2 SQLite: xamarin/sqlite/3.39.3@23e1ae7 Xamarin.Android Tools: xamarin/xamarin-android-tools/main@0be567a Microsoft Build of OpenJDK Java SDK: /Library/Java/JavaVirtualMachines/microsoft-11.jdk 11.0.12 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Eclipse Temurin JDK Java SDK: /Library/Java/JavaVirtualMachines/temurin-8.jdk 1.8.0.302 Android Designer EPL code available here: https://github.com/xamarin/AndroidDesigner.EPL Android SDK Manager Version: 17.4.0.54 Hash: 6eabb9e Branch: remotes/origin/d17-4 Build date: 2022-11-02 23:42:28 UTC Android Device Manager Version: 0.0.0.1206 Hash: 886af39 Branch: 886af39 Build date: 2022-11-02 23:42:28 UTC Build Information Release ID: 1704002406 Git revision: dfb45c1152306f0d5f77334f9d026b0aadb7ec90 Build date: 2022-11-02 23:40:15+00 Build branch: release-17.4 Build lane: release-17.4 Operating System Mac OS X 12.6.1 Darwin 21.6.0 Darwin Kernel Version 21.6.0 Thu Sep 29 20:12:57 PDT 2022 root:xnu-8020.240.7~1/RELEASE_X86_64 x86_64 ```

Reproduction Link

Workaround

Check Element for null in ApplyThemeIfNeeded of Renderer subclass.

P3PPP commented 1 year ago

Herer is the reproduction. MaterialApp.zip

durandt commented 5 months ago

Thank you for finding, documenting and fixing this @P3PPP (Note: Fixed in Service Release 14, Xamarin.Forms 5.0.0.2578 if I read correctly)

I am servicing an app that hasn't received any update for several months and stumbled upon a few occurrences of this crash in the crash analytics.