Xamarin.Forms icon indicating copy to clipboard operation
Xamarin.Forms copied to clipboard

[Bug] Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.'

Open alzubitariq opened this issue 4 years ago • 10 comments

Description

System.ObjectDisposedException: 'Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.'

[mono-rt] [ERROR] FATAL UNHANDLED EXCEPTION: System.ObjectDisposedException: Cannot access a disposed object. [mono-rt] Object name: 'Android.Webkit.WebView'. [mono-rt] at (wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.69(intptr,intptr,intptr,intptr,intptr) [mono-rt] at (wrapper native-to-managed) Android.Runtime.DynamicMethodNameCounter.69(intptr,intptr,intptr,intptr,intptr) [BpBinder] onLastStrongRef automatically unlinking death recipients:

Steps to Reproduce

Open a web page Execute an async javascript functions Click on button back navigation Repeat these steps quickly 3 or 4 times or until crash happen

Xamarin.Forms 5.0.0.2012

alzubitariq avatar Sep 29 '21 21:09 alzubitariq

For navigation use Shell or NavigationPage?, could you attach a small sample where reproduce the issue?. Thanks!

jsuarezruiz avatar Sep 30 '21 07:09 jsuarezruiz

I also have this problem, how to solve it

SolarisYan avatar Dec 23 '21 14:12 SolarisYan

Sorry for the late reply @jsuarezruiz

it is an NavigationPage let me picture some scenario , suppose I have two different pages

  • MainPage
  • WebViewPage

to catch this issue you need to speed up the process of clicking , for example open the WebViewPage and then click on back directly while the page in construction process , that is it

alzubitariq avatar Dec 23 '21 17:12 alzubitariq

Hello @SolarisYan how did u face this issue ! is there an specific behavior !!

alzubitariq avatar Dec 23 '21 17:12 alzubitariq

Hi, I have the same scenario.

I have a very simple WebViewPage, like below:

<?xml version="1.0" encoding="utf-8" ?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="MyApp.Views.ContactUs"> <ContentPage.Content> <StackLayout> <WebView x:Name="webView" Source="https://www.microsoft.com/" VerticalOptions="FillAndExpand" Navigated="webviewNavigated" Navigating="webviewNavigating"/> </StackLayout> </ContentPage.Content> </ContentPage>

From my MainPage (or any other page) I click a button and call this page like below await Shell.Current.GoToAsync("ContactUs"); If I wait to the entire page to be loaded, everything works great, but if I click back button without wait, after 3 or 4 times, the app crash with the message: Cannot access a disposed object. Object name: 'Android.Webkit.WebView'.

I hope this help to solve this issue. Any workaround will be very appreciated.

JairoMarques avatar Feb 08 '22 23:02 JairoMarques

For navigation use Shell or NavigationPage?, could you attach a small sample where reproduce the issue?. Thanks!

@jsuarezruiz i hope my post could help

JairoMarques avatar Feb 09 '22 19:02 JairoMarques

Same problem...

IShiraiKurokoI avatar May 20 '22 07:05 IShiraiKurokoI

Below is the stack trace I'm seeing reported.

@jsuarezruiz Could we maybe add some defensive code around this line to make it more stable? It looks like it's crashing in the accessor for Control.Url? I wonder if it's as simple as checking whether Control has been disposed before we try to access Url?

https://github.com/xamarin/Xamarin.Forms/blob/94acebbb4bee16bf71338ef6906b308ca08524c2/Xamarin.Forms.Platform.Android/Renderers/FormsWebViewClient.cs#L101

JniPeerMembers.AssertSelf (Java.Interop.IJavaPeerable self)
JniPeerMembers+JniInstanceMethods.InvokeVirtualObjectMethod (System.String encodedMember, Java.Interop.IJavaPeerable self, Java.Interop.JniArgumentValue* parameters)
WebView.get_Url ()
FormsWebViewClient.OnReceivedError (Android.Webkit.WebView view, Android.Webkit.IWebResourceRequest request, Android.Webkit.WebResourceError error)
WebViewClient.n_OnReceivedError_Landroid_webkit_WebView_Landroid_webkit_WebResourceRequest_Landroid_webkit_WebResourceError_ (System.IntPtr jnienv, System.IntPtr native__this, System.IntPtr native_view, System.IntPtr native_request, System.IntPtr native_error)
(wrapper dynamic-method) Android.Runtime.DynamicMethodNameCounter.16(intptr,intptr,intptr,intptr,intptr)

greg84 avatar Aug 20 '22 10:08 greg84

Did anyone manage to find fix?

kubalobacz avatar Aug 24 '22 12:08 kubalobacz

Having same issue as well.

dzebas avatar Sep 01 '22 06:09 dzebas

@kubalobacz @dzebas @jsuarezruiz i Have found a workaround

[assembly: ExportRenderer(typeof(WebView), typeof(CustomWebViewCustomRenderer))]
namespace <yournamespace>.Droid
{
    public class CustomWebViewCustomRenderer : WebViewRenderer
    {
        public CustomWebViewCustomRenderer(Context context) : base(context)
        {
        }

       protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                View = e.NewElement as CustomWebView;
                var webView = Control;
                  webView.SetWebViewClient(new MyWebViewClient(this));
               }
            }
        }

public class MyWebViewClient : FormsWebViewClient
        {
            public MyWebViewClient(WebViewRenderer renderer) : base(renderer)
            {
            }

            public override void OnReceivedError(Android.Webkit.WebView view, IWebResourceRequest request, WebResourceError error)
            {
                try
                {
                    base.OnReceivedError(view, request, error);
                }
                catch (System.Exception ex)
                {
                    System.Diagnostics.Debug.WriteLine(ex.ToString());
                }
                

            }
        }

giuseppenovielli avatar Dec 06 '22 11:12 giuseppenovielli