Memory Leak related to ChromiumWebBrowser.CleanupElement on tab opening/closing

536 views
Skip to first unread message

dmitry...@gmail.com

unread,
Feb 16, 2015, 12:10:19 PM2/16/15
to cefs...@googlegroups.com
Hi CefSharp group!

I've discovered that ChromiumWebBrowser control object is not garbage collected after tab closing in CefSharp.Wpf.Example project.
The version of CefSharp I use is CefSharp-33.0.2.
I've investigated it using JetBrains DotMemory and WinDbg and found out the following:

File: CefSharp.Wpf\ChromiumWebBrowser.cs

1. By default, CleanupElement is a Window that hosts CefSharp control:

        private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
        {
            if (CleanupElement == null)
            {
                CleanupElement = Window.GetWindow(this);
            }

            AddSourceHookIfNotAlreadyPresent();
        }

2. Then, we subscribe to Unloaded event of CleanupElement and never unsubscribe:

        protected virtual void OnCleanupElementChanged(FrameworkElement oldValue, FrameworkElement newValue)
        {
            if (oldValue != null)
            {
                oldValue.Unloaded -= OnCleanupElementUnloaded;
            }

            if (newValue != null)
            {
                newValue.Unloaded -= OnCleanupElementUnloaded;
                newValue.Unloaded += OnCleanupElementUnloaded;
            }
        }


This makes Window to hold a reference to ChromiumWebBrowser control through FrameworkElement.Unloaded event subscription.

The solution that I use is to add the following code to Dispose method:

            if(CleanupElement != null)
            {
                CleanupElement.Unloaded -= OnCleanupElementUnloaded;
            }


Dmitry

Jørn Hansen

unread,
Feb 16, 2015, 3:26:11 PM2/16/15
to cefs...@googlegroups.com
Hi Dmitry,

Thanks for digging into this and sharing your findings. That's much appreciated! Quite recently a PR was merged to `master` that will in hopefully not to long go out as our 39.0.0 release:
https://github.com/cefsharp/CefSharp/pull/786

If you still have DotMemory etc. in hand :-) it would be very welcome if you could check out if that PR fixes the issue or we still have the leak for the scenario you describe.

You can either grab the latest CI build at https://www.myget.org/F/cefsharp/ or build master from source.

--
Thanks,
JornH

Alex Maitland

unread,
Feb 17, 2015, 3:37:27 AM2/17/15
to cefs...@googlegroups.com
Quick look using the WPF example and I'm not sure if `OnCleanupElementUnloaded ` actually gets called, so the whole feature may need a rethink.

Short term, I think we should unhook the event handler. Long term we may need to evaluate.

Dmitry: Any chance you could submit a PR to our github project?

dmitry...@gmail.com

unread,
Feb 17, 2015, 4:09:17 AM2/17/15
to cefs...@googlegroups.com
Hi Alex
Ok, let me try to submit PR.

dmitry...@gmail.com

unread,
Feb 17, 2015, 7:38:36 AM2/17/15
to cefs...@googlegroups.com
Hi Jorn

I've just checked this scenario on CefSharp-39.0.0-pre02 and I can still see the memory leak and DotMemory shows the same reason - ChromiumWebBrowser object is held by RoutedEventHandler, which is  a subscription to Window.Unloaded routed event.

Dmitry

dmitry...@gmail.com

unread,
Feb 20, 2015, 7:32:47 AM2/20/15
to cefs...@googlegroups.com
Hi guys,
Here's my pull request: https://github.com/cefsharp/CefSharp/pull/827.
I'm new to github so please correct me if I've done something not the way that it's supposed to be done.

Dmitry
Reply all
Reply to author
Forward
0 new messages