diff --git a/include/wx/msw/private/comptr.h b/include/wx/msw/private/comptr.h index 2b21cfa82e..db8c9bcf95 100644 --- a/include/wx/msw/private/comptr.h +++ b/include/wx/msw/private/comptr.h @@ -100,6 +100,11 @@ public: return m_ptr; } + T* Get() const + { + return m_ptr; + } + bool operator<(T* ptr) const { return get() < ptr; diff --git a/src/msw/webview_edge.cpp b/src/msw/webview_edge.cpp index 603d7a8387..866c2b3eb0 100644 --- a/src/msw/webview_edge.cpp +++ b/src/msw/webview_edge.cpp @@ -93,19 +93,19 @@ template < typename baseT, typename lambdaT, // base type & lambda type typename LT, typename ...argTs // for capturing argument types of lambda > -baseT *callback_impl(lambdaT&& lambda, HRESULT (LT::*)(argTs...) const) +wxCOMPtr Callback_impl(lambdaT&& lambda, HRESULT (LT::*)(argTs...) const) { - return new CInvokableLambda(lambda); + return wxCOMPtr(new CInvokableLambda(lambda)); } template -baseT *callback(lambdaT&& lambda) +wxCOMPtr Callback(lambdaT&& lambda) { - return callback_impl(std::move(lambda), &lambdaT::operator()); + return Callback_impl(std::move(lambda), &lambdaT::operator()); } template -baseT *callback(contextT *ctx, HRESULT (contextT::*mthd)(argTs...)) +wxCOMPtr Callback(contextT *ctx, HRESULT (contextT::*mthd)(argTs...)) { - return new CInvokableMethod(ctx, mthd); + return wxCOMPtr(new CInvokableMethod(ctx, mthd)); } wxIMPLEMENT_DYNAMIC_CLASS(wxWebViewEdge, wxWebView); @@ -170,8 +170,8 @@ bool wxWebViewEdgeImpl::Create() ms_browserExecutableDir.wc_str(), userDataPath.wc_str(), nullptr, - callback(this, - &wxWebViewEdgeImpl::OnEnvironmentCreated)); + Callback(this, + &wxWebViewEdgeImpl::OnEnvironmentCreated).Get()); if (FAILED(hr)) { wxLogApiError("CreateWebView2EnvironmentWithOptions", hr); @@ -187,8 +187,8 @@ HRESULT wxWebViewEdgeImpl::OnEnvironmentCreated( environment->QueryInterface(IID_PPV_ARGS(&m_webViewEnvironment)); m_webViewEnvironment->CreateCoreWebView2Controller( m_ctrl->GetHWND(), - callback( - this, &wxWebViewEdgeImpl::OnWebViewCreated)); + Callback( + this, &wxWebViewEdgeImpl::OnWebViewCreated).Get()); return S_OK; } @@ -417,28 +417,28 @@ HRESULT wxWebViewEdgeImpl::OnWebViewCreated(HRESULT result, ICoreWebView2Control // Connect and handle the various WebView events m_webView->add_NavigationStarting( - callback( - this, &wxWebViewEdgeImpl::OnNavigationStarting), + Callback( + this, &wxWebViewEdgeImpl::OnNavigationStarting).Get(), &m_navigationStartingToken); m_webView->add_SourceChanged( Callback( this, &wxWebViewEdgeImpl::OnSourceChanged).Get(), &m_sourceChangedToken); m_webView->add_NavigationCompleted( - callback( - this, &wxWebViewEdgeImpl::OnNavigationCompleted), + Callback( + this, &wxWebViewEdgeImpl::OnNavigationCompleted).Get(), &m_navigationCompletedToken); m_webView->add_NewWindowRequested( - callback( - this, &wxWebViewEdgeImpl::OnNewWindowRequested), + Callback( + this, &wxWebViewEdgeImpl::OnNewWindowRequested).Get(), &m_newWindowRequestedToken); m_webView->add_DocumentTitleChanged( - callback( - this, &wxWebViewEdgeImpl::OnDocumentTitleChanged), + Callback( + this, &wxWebViewEdgeImpl::OnDocumentTitleChanged).Get(), &m_documentTitleChangedToken); m_webView->add_ContentLoading( - callback( - this, &wxWebViewEdgeImpl::OnContentLoading), + Callback( + this, &wxWebViewEdgeImpl::OnContentLoading).Get(), &m_contentLoadingToken); m_webView->add_ContainsFullScreenElementChanged( Callback( @@ -786,7 +786,7 @@ bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output) bool scriptExecuted = false; // Start script execution - HRESULT executionResult = m_impl->m_webView->ExecuteScript(javascript.wc_str(), callback( + HRESULT executionResult = m_impl->m_webView->ExecuteScript(javascript.wc_str(), Callback( [&scriptExecuted, &executionResult, output](HRESULT error, PCWSTR result) -> HRESULT { // Handle script execution callback @@ -801,7 +801,7 @@ bool wxWebViewEdge::RunScriptSync(const wxString& javascript, wxString* output) scriptExecuted = true; return S_OK; - })); + }).Get()); // Wait for script exection while (!scriptExecuted)