[JUCE 8 WebView BUG?] WebViewPluginDemo Crashes When Repeatedly Launched

Hello!!

The newly released JUCE 8 is fantastic, and developing with JUCE is a truly enjoyable experience! I am experimenting with the new WebViewPluginDemo to see if I can build my plugin’s UI using web technologies.

While creating my plugin based on the WebViewPluginDemo, I noticed something. When the Timer in WebViewPluginDemo is set to 30Hz, and the plugin is repeatedly opened and closed, it consistently crashes.

The steps to reproduce this are as follows:

  1. From Projucer, go to Open Examples → Plugins → WebViewPluginDemo and set up the project.
  2. Open Visual Studio 2022 and change the Timer execution frequency to 30Hz. Specifically, at the location indicated in this link, set startTimerHz (30);
    JUCE/examples/Plugins/WebViewPluginDemo.h at master · juce-framework/JUCE · GitHub
  3. Build the project and load the VST3 in the AudioPluginHost included with JUCE.
  4. Repeatedly open and close the plugin window. After a few repetitions, it will crash.

Attached is an image of the call stack result from Visual Studio 2022. Since part of it is cut off, I have included the entire call stack below.

>	WebViewPluginDemo.vst3!juce::ReferenceCountedObjectPtr<juce::AsyncUpdater::AsyncUpdaterMessage>::operator->() Line 406	C++
 	WebViewPluginDemo.vst3!juce::AsyncUpdater::triggerAsyncUpdate() Line 80	C++
 	WebViewPluginDemo.vst3!juce::WebBrowserComponent::Impl::Platform::WebView2::createWebView::__l5::<lambda>(HRESULT __formal, ICoreWebView2Controller * controller) Line 1103	C++
 	[External Code]	
 	AudioPluginHost.exe!juce::InternalMessageQueue::dispatchNextMessage(bool returnIfNoPendingMessages) Line 151	C++
 	AudioPluginHost.exe!juce::detail::dispatchNextMessageOnSystemQueue(bool returnIfNoPendingMessages) Line 274	C++
 	AudioPluginHost.exe!juce::MessageManager::runDispatchLoop() Line 124	C++
 	AudioPluginHost.exe!juce::JUCEApplicationBase::main() Line 277	C++
 	AudioPluginHost.exe!WinMain(HINSTANCE__ * __formal, HINSTANCE__ * __formal, char * __formal, int __formal) Line 391	C++
 	[External Code]	

As a clue to solving the problem, it seems that this crash is caused by the following code, but I can’t figure it out with my current knowledge. I’m not sure the solution.

// from https://github.com/juce-framework/JUCE/blob/master/modules/juce_gui_extra/native/juce_WebBrowserComponent_windows.cpp#L1103

if (! weakThis->webView2ConstructionHelper.viewsWaitingForCreation.empty())
    (*weakThis->webView2ConstructionHelper.viewsWaitingForCreation.begin())->triggerAsyncUpdate();

How can this be resolved?

I was unable to reproduce this issue on my machine, so there’s a bit of guesswork here.

Can you modify the WebView2 destructor at line 520 by adding a cancelPendingUpdate() call? It should look like this

~WebView2() override
{
    cancelPendingUpdate();
    removeEventHandlers();
    closeWebView();
}

Can you check please if you still see crashes with this change?

Thank you for the quick response! I tried it, but the crash wasn’t resolved.

To increase this crash occurring, could you set the Timer execution cycle of WebViewPluginDemo to 60Hz and try again? It’s startTimerHz(60);.
The attached video is a screen recording that reproduces this crash. I hope it will be helpful.

Also, I found that if webComponent.emitEventIfBrowserIsVisible("spectrumData", var{}); is commented out, this crash does not occur. It seems that when emitEventIfBrowserIsVisible() is executed frequently, it might crash.

Thank you for the detailed demonstration.

I managed to trigger the issue and I think I have a solution. Given how rarely this happens though, I would appreciate if you could see if this solves the issue for you as well.

I added some more changes to ~WebView2()

~WebView2() override
{
    if (webView2ConstructionHelper.webView2BeingCreated == this)
        webView2ConstructionHelper.webView2BeingCreated = nullptr;

    webView2ConstructionHelper.viewsWaitingForCreation.erase (this);

    cancelPendingUpdate();
    removeEventHandlers();
    closeWebView();
}

Thank you for exploring the fix! I tried it on my end as well, and it works perfectly now. No matter how many times I open and close the plugin window, it doesn’t crash anymore!

I will be using this fix for the time being. Looking forward to seeing the commits to the master or develop branch of JUCE!

2 Likes

The fix has been released on develop

2 Likes

Thank you very much!!