-
Notifications
You must be signed in to change notification settings - Fork 1.9k
[Bug] Android: Crash when user turns on the dark theme (ObjectDisposedException) #11704
Comments
@holecekp can you attach a repro please? @StephaneDelcroix FYI possible 4.8 regression |
I will try to prepare a repro but it will be hard. I still do not know which steps are necessary for the crash to happen in my full project. The crash happens randomly. Sometimes it crashes twice in a minute; sometimes I repeat the same steps for 10 minutes without crash. I verified that the crash appears also in the debug version in the emulator. |
One more note that might be important for finding the cause of this bug. I looked at the stack trace and the crash is in the UpdateImage method of ButtonLayoutManager The important thing is that I have am using AppThemeBinding to set the ImageSource property as follows: var imageSourceLight = new FileImageSource { File = androidImageFileName };
var imageSourceDark = new FileImageSource { File = androidImageFileName };
button.SetOnAppTheme(Button.ImageSourceProperty, imageSourceLight, imageSourceDark); It might look weird to set the same file name using AppThemeBinding, but it is necessary. If AppThemeBinding is not used, Android chooses the correct version of the image from drawable, or drawable-night folders but it does not update it immediately (the user has to select another page in Shell and return back). With AppThemeBinding, the icon is updated immediately after the theme is changed. |
It seems that the crash is caused by a button in a DataTemplate that is used for a CollectionView. When I load different data into the CollectionView for 5 times and then turn on the dark mode, the app always crashes. |
I have created a repro project: Steps for reproducing the crash:
This bug is not only in XF 4.8. This steps lead to crash in all versions that contain AppThemeBinding (including 4.6.0.967)! |
unlikely to be related to theme. if this error happens, it could be triggered from user code too, I guess |
I just had this in a Xamarin Shell app. I have a page with an Entry on which uses AppThemeBinding. If I navigate to that page, then to another, then switch to Dark Mode I get the following error:
It seems that Xamarin Shell is disposing the Entry but the page is still in memory and the AppThemeBinding is still firing - which as the Entry is disposed is throwing an error. |
Hi, having the same issue on Android. Xamarin.Forms 4.8.0.1560 |
I am also having the same issue but it's so random I can't catch it |
I have the same issue in Xamarin Forms 5, not just when switching to dark mode but also back to light mode. |
I am getting this crash as well. It happens when switching the app theme, but randomly. Dozens of crashes so far. Trace from Microsoft AppCenter crash reporting: |
I think I found a workaround. I have a background service that refreshes the CollectionView list (of numbers) every few seconds. I pause the background update for 2 seconds, Task.Delay(100) to allow any update to finish, then set the AppTheme. No crashes so far. |
@mrengineer7777 Unfortunately, this is not a workaround for all cases. In my case, nothing is refreshed periodically. All updates of the UI are done as a result of a user action (clicking a button, etc.). So there is nothing that I could pause. Also, the repro project that I have posted before is not doing any background updates at the time when the theme is changed and the app crashes. |
We may be dealing with slightly different issues. Your repro states that it crashes after changing the system to dark mode. Unfortunately my test devices are Android 7 and 8 (no system option for dark). The crash on mine was happening when the user manually changed the theme in the app. |
@holecekp I loaded your repro and tested on emulator Pixel 3 XL Q Android 10. I was eventually able to get it to crash after switching the OS from dark to light mode (I tried multiple times). Crash: Something to investigate: I turned off the option "Use the concurrent garbage collector" on your repro and now I can't get it to crash. Edit: spoke too soon. I eventually got it to crash. |
@holecekp I don't know if this is an option for you, but I just updated your repro to the latest Nugets. XF 5.0.0.2012 and Essentials 1.6.1. I also changed the target framework to Android 11 and the Target Android version to 11. I couldn't get any crashes on the Pixel 3. Lots to play with there. Sorry I can't give you any more time. Must get back to my project. |
So much for no crashes. Sigh. My app crashes 90% of the time now when switching the app theme on Android. I'll work on a repro tomorrow (hopefully an update of @holecekp's project). This is definitely related to the Android renderer trying to reference items that have been GC'd. Point of interest: calling GC.Collect() before setting the app theme causes a crash 100% of the time on my project. System.ObjectDisposedException: 'Cannot access a disposed object. |
@holecekp @PureWeen @samhouts Updated project with latest and greatest Nugets. 100% crash repro attached. Run on emulator "Pixel 3 XL Q 10.0 - API 29". Click on [Load Data] 3 times. Click [Dark]. Click on [Load Data] 3 times. Click [Light]. You will get an app crash on either the Dark or Light click. In this case the error is: The exact element faulted varies by project. |
More info: on my project crash on AppTheme change happens consistently on Android 8.1, but not on Android 7.1.1. Also it only happens if the user interacts with (eg taps) the CollectionView first. |
Also reporting this issue. My AppThemeBindings are in the App.xaml. In my app I have a CollectionView, on selecting an item the app will navigate to an item details page. My repro steps:
Crash:
Emulator: Pixel 2 Q 10.0 - API 29 |
Hi, having the same issue on Android but worked in ios |
Any solution? @PureWeen |
Hi, GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Application.Current.UserAppTheme = OSAppTheme.Light; So all the previously visited and closed views/pages are collected and when AppThemeBinding kicks in there are no theme updates on disposed views and no crash. I have no idea how to fix this. Perhaps @jfversluis has an idea. |
The initial stack traces mentions the button, we implemented a fix for that I think, can someone confirm this is still happening in the latest Forms version (SR7 5.0.0.2244)? |
This can happen everywhere, when AppThemeBinding fires the OnPropertyChanged Event. Mostly it changes a color. For example BackgroundColor. |
Something has changed. Perhaps an Xamrin.Android SDK Update. I'm unable to reproduce the exception anymore. |
Sounds good! Anyone finding this, please make sure to update to all the latest bits. If you can still reproduce with that, let me know! |
Same issue with Xamarin.Forms 5.0.0.2337 on Android 12 |
This crash is still in the latest Xamarin.Forms (5.0.0 SR 14) |
Description
I have upgraded Xamarin.Forms to 4.8 and I am experiencing occasional crashes when I switch from light theme to the dark one in the Android settings.
I am including the output from Logcat:
The buttons have colors set using Styles and AppThemeBinding as follows:
Steps to Reproduce
The crash occurs randomly. Sometimes it crashes, sometimes it does not. I have experienced 2 crashes on my device when I did the following:
Update: I have created a repro project and described the steps to reproduce this bug. It is below in the discussion to this issue.
Expected Behavior
The app shoud not crash when the user changes the theme.
Actual Behavior
The app sometimes crashes when the user turns on the dark theme.
Basic Information
Version with issue: 4.8.0.1269
Last known good version: Probably 4.7.0.1239 which I used in the last version of my app. Since the crash occurs only sometimes, it is hard to tell which is tha last good version.
IDE: Visual Studio 2019
Platform Target Frameworks
Nuget Packages:
Xamarin.Forms (4.8.0.1269)
Xamarin.Essentials (1.5.3.2)
Xamarin.Android.Support.Design (28.0.0.3)
Xamarin.Android.Support.v7.AppCompat (28.0.0.3)
Xamarin.Android.Support.v4 (28.0.0.3)
Xamarin.Android.Support.v7.CardView (28.0.0.3)
Xamarin.Android.Support.v7.MediaRouter (28.0.0.3)
Xamarin.AndroidX.AppCompat.Resources (1.1.0.1)
Xamarin.AndroidX.MediaRouter (1.1.0.1)
Affected Devices: Tested on Xiaomi Mi A2 Lite (Android 10)
Workaround
Avoid using CollectionView. For example Stacklayout with BindableLayout.ItemsSource does not crash and it can be used in very simple cases. However, if you need any feature from CollectionView, then there is no workaround.
The text was updated successfully, but these errors were encountered: