Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

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

Open
alzubitariq opened this issue Sep 29, 2021 · 11 comments
Labels
a/navigation a/webview p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛

Comments

@alzubitariq
Copy link

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 alzubitariq added s/unverified New report that has yet to be verified t/bug 🐛 labels Sep 29, 2021
@jsuarezruiz
Copy link
Contributor

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

@jsuarezruiz jsuarezruiz added a/webview p/Android a/navigation s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. labels Sep 30, 2021
@SolarisYan
Copy link

I also have this problem, how to solve it

@alzubitariq
Copy link
Author

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
Copy link
Author

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

@JairoMarques
Copy link

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
Copy link

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

@jsuarezruiz i hope my post could help

@IShiraiKurokoI
Copy link

Same problem...

@greg84
Copy link

greg84 commented Aug 20, 2022

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?

if (request.Url.ToString() == _renderer?.Control.Url)

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)

@kubalobacz
Copy link

Did anyone manage to find fix?

@dzebas
Copy link

dzebas commented Sep 1, 2022

Having same issue as well.

@giuseppenovielli
Copy link

giuseppenovielli commented Dec 6, 2022

@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());
                }
                

            }
        }

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
a/navigation a/webview p/Android s/needs-info ❓ A question has been asked that requires an answer before work can continue on this issue. s/unverified New report that has yet to be verified t/bug 🐛
Projects
None yet
Development

No branches or pull requests

9 participants