WebBrowser crash when events raised on the OnFocusedNodeChanged

Issue #158 resolved
Geevo
created an issue

Hi i'm trying to hook onto the above event for focus changes on a webpage. Subscribing to the event causing an app crash when an element loses focus.

Go to Google. Click on the search input textbox to give focus, then take focus away by clicking any white space area.

I could be doing this wrong, however I'm not sure how to hook on to the RenderProcessHandler via the ChromiumWebBrowser control without subscribing to the RemoteProcessCreated first.

Added the following to the BrowserForm.cs CfxTestApplication

ChromiumWebBrowser.RemoteProcessCreated += (e) =>
{
    e.RenderProcessHandler.OnFocusedNodeChanged += (s, ev) =>
    {
         //no logic required here
    };
};

Any help would be much appreciated

Comments (5)

  1. Geevo reporter

    Further to this, i decided to make amendments to the WebBrowser control to forward the events, similar to the OnV8ContextCreated, however i still stumbled over with the "CfxTestApplication has stopped working" crash again. Doh!

    namespace Chromium.WebBrowser {
        internal class RenderProcessHandler : CfrRenderProcessHandler {
    
            private readonly RenderProcess remoteProcess;
    
            internal RenderProcessHandler(RenderProcess remoteProcess) {
                this.remoteProcess = remoteProcess;
    
                this.OnContextCreated += RenderProcessHandler_OnContextCreated;
                this.OnBrowserCreated += RenderProcessHandler_OnBrowserCreated;
                this.OnFocusedNodeChanged += RenderProcessHandler_OnFocusedNodeChanged; // subscribed to the event to forward
            }
    
            private void RenderProcessHandler_OnFocusedNodeChanged(object sender, CfrOnFocusedNodeChangedEventArgs e)
            {
    
            } .
    ...
    

    According to the debug the crash occured here...

    // System.AccessViolationException: 'Attempted to read or write protected memory. This is often an indication that other memory is corrupt.'
    
    // CfrRuntime.cs
    
    // Line 72
    
    protected override void RemoteProcedure() {
                switch(CfxApi.PlatformOS) {
                    case CfxPlatformOS.Windows:
                        __retval = CfxApi.Runtime.cfx_execute_process(IntPtr.Zero, application, IntPtr.Zero); // ** Crash here **
                        return;
                    case CfxPlatformOS.Linux:
                        using(var mainArgs = CfxMainArgs.ForLinux()) {
                            __retval = CfxApi.Runtime.cfx_execute_process(CfxMainArgs.Unwrap(mainArgs), application, IntPtr.Zero);
                            mainArgs.mainArgsLinux.Free();
                        }
                        return;
                    default:
                        throw new CfxException("Unsupported platform.");
                }
            }
    
  2. wborgsm

    Fixed - see commits 0c6c3bb and 0fe8a1b. This was a null pointer dereference which might also have happened in other handler callbacks when a CEF struct was null. Just never happened before, but in OnFocusedNodeChanged, when no node is focused, CEF submits a null pointer to the client application.

    Btw. the preferred way to subscribe to render process events is ChromiumWebBrowser.RemoteProcessCreated and there is no need to subscribe in the RenderProcessHandler because the ChromiumWebBrowser library doesn't need it. With the fix, it should just work now (works fine in the test application).

    Thanks again for reporting this.

  3. Log in to comment