[Android] ObjectDisposedException in MauiMaterialButton
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 296
Description
### Description
I've just released a new version of my app migrated from Xamarin.Forms to .NET MAUI. AppCenter.Crashes started immediately report crashes caused by ObjectDisposedException in MauiMaterialButton.
```
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Microsoft.Maui.Platform.MauiMaterialButton'.
at Java.Interop.JniPeerMembers.AssertSelf(IJavaPeerable self) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.cs:line 153
at Java.Interop.JniPeerMembers.JniInstanceMethods.InvokeVirtualVoidMethod(String encodedMember, IJavaPeerable self, JniArgumentValue* parameters) in /Users/runner/work/1/s/xamarin-android/external/Java.Interop/src/Java.Interop/Java.Interop/JniPeerMembers.JniInstanceMethods_Invoke.cs:line 57
at Android.Views.View.SetOnClickListener(IOnClickListener l) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net9.0/android-35/mcw/Android.Views.View.cs:line 22712
at Microsoft.Maui.Handlers.ButtonHandler.DisconnectHandler(MaterialButton platformView) in /_/src/Core/src/Handlers/Button/ButtonHandler.Android.cs:line 56
at Microsoft.Maui.Handlers.ViewHandler`2[[Microsoft.Maui.IButton, Microsoft.Maui, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[Google.Android.Material.Button.MaterialButton, Xamarin.Google.Android.Material, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].OnDisconnectHandler(View platformView) in /_/src/Core/src/Handlers/View/ViewHandlerOfT.cs:line 79
at Microsoft.Maui.Handlers.ViewHandler.OnDisconnectHandler(Object platformView) in /_/src/Core/src/Handlers/View/ViewHandler.cs:line 219
at Microsoft.Maui.Handlers.ElementHandler.DisconnectHandler(Object platformView) in /_/src/Core/src/Handlers/Element/ElementHandler.cs:line 112
at Microsoft.Maui.Handlers.ElementHandler.Microsoft.Maui.IElementHandler.DisconnectHandler() in /_/src/Core/src/Handlers/Element/ElementHandler.cs:line 130
at Microsoft.Maui.ViewExtensions.DisconnectHandlers(IView view) in /_/src/Core/src/ViewExtensions.cs:line 44
at Microsoft.Maui.Controls.BindableLayoutController.CreateChildren() in /_/src/Controls/src/Core/BindableLayout/BindableLayout.cs:line 403
at Microsoft.Maui.Controls.BindableLayoutController.SetItemsSource(IEnumerable itemsSource) in /_/src/Controls/src/Core/BindableLayout/BindableLayout.cs:line 293
at Microsoft.Maui.Controls.BindableLayoutController.set_ItemsSource(IEnumerable value) in /_/src/Controls/src/Core/BindableLayout/BindableLayout.cs:line 251
at Microsoft.Maui.Controls.BindableLayout.<>c.<.cctor>b__25_0(BindableObject b, Object o, Object n) in /_/src/Controls/src/Core/BindableLayout/BindableLayout.cs:line 24
at Microsoft.Maui.Controls.BindableObject.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean didChange, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/BindableObject.cs:line 676
at Microsoft.Maui.Controls.Element.OnBindablePropertySet(BindableProperty property, Object original, Object value, Boolean changed, Boolean willFirePropertyChanged) in /_/src/Controls/src/Core/Element/Element.cs:line 642
at Microsoft.Maui.Controls.BindableObject.SetValueActual(BindableProperty property, BindablePropertyContext context, Object value, Boolean currentlyApplying, SetValueFlags attributes, SetterSpecificity specificity, Boolean silent) in /_/src/Controls/src/Core/BindableObject.cs:line 663
at Microsoft.Maui.Controls.BindableObject.SetValueCore(BindableProperty property, Object value, SetValueFlags attributes, SetValuePrivateFlags privateAttributes, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindableObject.cs:line 591
at Microsoft.Maui.Controls.BindingExpression.ApplyCore(Object sourceObject, BindableObject target, BindableProperty property, Boolean fromTarget, SetterSpecificity specificity) in /_/src/Controls/src/Core/BindingExpression.cs:line 179
at Microsoft.Maui.Controls.BindingExpression.Apply(Boolean fromTarget) in /_/src/Controls/src/Core/BindingExpression.cs:line 60
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.b__50_0() in /_/src/Controls/src/Core/BindingExpression.cs:line 712
at Microsoft.Maui.Controls.DispatcherExtensions.DispatchIfRequired(IDispatcher dispatcher, Action action) in /_/src/Controls/src/Core/DispatcherExtensions.cs:line 59
at Microsoft.Maui.Controls.BindingExpression.BindingExpressionPart.PropertyChanged(Object sender, PropertyChangedEventArgs args) in /_/src/Controls/src/Core/BindingExpression.cs:line 712
at Microsoft.Maui.Controls.BindingExpression.WeakPropertyChangedProxy.OnPropertyChanged(Object sender, PropertyChangedEventArgs e) in /_/src/Controls/src/Core/BindingExpression.cs:line 613
at MorseCode.ViewModels.ViewModelBase.OnPropertyChanged(String propertyName) in D:\dotNET\MorseCode\MorseCode\ViewModels\ViewModelBase.cs:line 103
at MorseCode.ViewModels.LearningViewModel.set_Level(LearningLevel value) in D:\dotNET\MorseCode\MorseCode\ViewModels\LearningViewModel.cs:line 472
at MorseCode.ViewModels.LearningViewModel.GoToNextLevel() in D:\dotNET\MorseCode\MorseCode\ViewModels\LearningViewModel.cs:line 971
at CommunityToolkit.Mvvm.Input.AsyncRelayCommand.AwaitAndThrowIfFailed(Task executionTask) in /_/src/CommunityToolkit.Mvvm/Input/AsyncRelayCommand.cs:line 351
at System.Threading.Tasks.Task.<>c.b__128_0(Object state)
at Android.App.SyncContext.<>c__DisplayClass2_0.b__0() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.App/SyncContext.cs:line 36
at Java.Lang.Thread.RunnableImplementor.Run() in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Java.Lang/Thread.cs:line 37
at Java.Lang.IRunnableInvoker.n_Run(IntPtr jnienv, IntPtr native__this) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/obj/Release/net9.0/android-35/mcw/Java.Lang.IRunnable.cs:line 59
at Android.Runtime.JNINativeWrapper.Wrap_JniMarshal_PP_V(_JniMarshal_PP_V callback, IntPtr jnienv, IntPtr klazz) in /Users/runner/work/1/s/xamarin-android/src/Mono.Android/Android.Runtime/JNINativeWrapper.g.cs:line 22
```
I'm not able to reproduce these crashes locally in a deterministic way. It crashed once also on my computer (that is why I'm able to send the stack trace with the line numbers) but I was not able to reproduce it again. The issue seems to occur randomly. It is a .NET MAUI issue because Xamarin.Forms haven't ever crashed with a simmlar exception there.
### Steps to Reproduce
Sorry, I'm just receiving reports from AppCenter but I'm not able to reproduce it on my computer in a reliable way.
### Link to public reproduction project repository
_No response_
### Version with bug
9.0.80 SR8
### Is this a regression from previous behavior?
Yes, this used to work in Xamarin.Forms
### Last version that worked well
Unknown/Other
### Affected platforms
Android
### Affected platform versions
Android 13, Android 9
### Did you find any workaround?
No
### Relevant log output
```shell
```
Contributor guide
Research direction
Start by inspecting src/Core/src/Handlers/Button/ButtonHandler.Android.cs at DisconnectHandler and the BindableLayoutController.CreateChildren path shown in the stack trace. Investigate the intermittent handler-disconnect scenario on Android 9 and 13, then verify that updating the affected path prevents ObjectDisposedException for MauiMaterialButton; no reproduction project or deterministic steps are provided.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, csharp
- Domain
- mobile-dev
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100