Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unhandled "Cannot access a disposed object" exceptions occurs randomly #1494

Closed
DominikPalo opened this issue Dec 16, 2015 · 6 comments
Closed
Milestone

Comments

@DominikPalo
Copy link
Contributor

I'm using CefSharp (v45.0.0) in WPF app (WPF Chromium control) and my app sometimes crashes with an exception "Cannot access a disposed object". It happens during creating (opening) a new WPF window with the Chromium control.

"Cannot access a disposed object.
Object name: 'CefSharp.Internals.BrowserProcessServiceHost'.    
at System.ServiceModel.Dispatcher.ChannelDispatcherCollection.InsertItem(Int32 index, ChannelDispatcherBase item)   
at System.Collections.Generic.SynchronizedCollection`1.Add(T item)   
at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)   
at System.ServiceModel.ServiceHostBase.InitializeRuntime()   
at System.ServiceModel.ServiceHostBase.OnBeginOpen()   
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)   
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)   
at CefSharp.ManagedCefBrowserAdapter.OnAfterBrowserCreated(Int32 browserId)   
at CefSharp.Internals.ClientAdapter.OnAfterCreated(ClientAdapter* , CefRefPtr<CefBrowser>* browser)"
@DominikPalo DominikPalo changed the title "Cannot access a disposed object" occurs randomly Unhandled "Cannot access a disposed object" exceptions occurs randomly Dec 16, 2015
@amaitland
Copy link
Member

What other information can you provide? What does your code look like? Anything you might be doing slightly out of the ordinary? Ideally we need a way to reproduce the problem.

Do you use RegisterJsObject? If not you can disable the WCF service and get around the problem.

@DominikPalo
Copy link
Contributor Author

Yes, I'm using RegisterJsObject in the constructor of containing window (and in 99% of attempts it works without any problem):

    public ChatWindow()
    {
        InitializeComponent();
        Owner = Application.Current.MainWindow;

        Messenger.Default.Register<NotificationMessage>(this, (message) =>
        {
            DispatcherHelper.CheckBeginInvokeOnUI(() =>
            {
                if (message.Notification == "CloseWindowsBoundToMe" 
                    && message.Sender == DataContext)
                {
                    Close();
                }
            });

        });

        browser?.RegisterJsObject("bound", (DataContext as ChatWindowViewModel).Bound);
    }

And XAML:

<Window x:Class="Client.Windows.ChatWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
        mc:Ignorable="d" DataContext="{Binding ChatWindow, Source={StaticResource Locator}}"
        ResizeMode="NoResize" WindowStartupLocation="CenterOwner"
        Icon="/slido;component/slido.ico" WindowStyle="ToolWindow"
        Title="LiveAgent" SizeToContent="WidthAndHeight">

    <cefSharp:ChromiumWebBrowser Width="350" Height="450" x:Name="browser" WebBrowser="{Binding WebBrowser, Mode=OneWayToSource}" 
Address="{Binding ChatUrl}" />

</Window>

@amaitland
Copy link
Member

Does Close ever get called just after the Window has been opened?

@DominikPalo
Copy link
Contributor Author

No, the Close event is called from inner JavaScript (using a Bound object) only after user clicks on "Close" HTML element/control displayed on web.

@amaitland
Copy link
Member

amaitland added a commit that referenced this issue Jan 12, 2016
amaitland added a commit that referenced this issue Jan 12, 2016
@amaitland amaitland added this to the 47.0.0 milestone Jan 12, 2016
@amaitland
Copy link
Member

Added a simple try/catch around the Open call. Change will be in 47.0.0 release.

86b5c54

If you can reproduce with that change then this can be reopened. (Ideally investigate further and submit a PR with a proposed solution).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants