diff --git a/src/webenginewidgets/api/qwebenginepage.cpp b/src/webenginewidgets/api/qwebenginepage.cpp
index 0d556f15be44d4ba85684fd92c9acd20c1529b99..b35bb54ecf75e4c40e7343f997d7e386ef0642b6 100644
--- a/src/webenginewidgets/api/qwebenginepage.cpp
+++ b/src/webenginewidgets/api/qwebenginepage.cpp
@@ -288,8 +288,6 @@ void QWebEnginePagePrivate::adoptNewWindow(WebContentsAdapter *newWebContents, W
 
     // Overwrite the new page's WebContents with ours.
     if (newPage->d_func() != this) {
-        // Keep the old adapter referenced until the scriptcollection is rebound
-        QExplicitlySharedDataPointer<WebContentsAdapter> oldWebContents = newPage->d_func()->adapter;
         newPage->d_func()->adapter = newWebContents;
         newWebContents->initialize(newPage->d_func());
         newPage->d_func()->scriptCollection.d->rebindToContents(newWebContents);
@@ -455,9 +453,6 @@ void QWebEnginePagePrivate::recreateFromSerializedHistory(QDataStream &input)
 {
     QExplicitlySharedDataPointer<WebContentsAdapter> newWebContents = WebContentsAdapter::createFromSerializedNavigationHistory(input, this);
     if (newWebContents) {
-        // Keep the old adapter referenced so the user-scripts are not
-        // unregistered immediately.
-        QExplicitlySharedDataPointer<WebContentsAdapter> oldWebContents = adapter;
         adapter = newWebContents.data();
         adapter->initialize(this);
         if (webChannel)
diff --git a/src/webenginewidgets/api/qwebenginescriptcollection.cpp b/src/webenginewidgets/api/qwebenginescriptcollection.cpp
index 117c35b5aea8a54826b3f24af6217bdd8a51ee98..8d581b60a3bc0f8b75aa2c713188ab764a263c00 100644
--- a/src/webenginewidgets/api/qwebenginescriptcollection.cpp
+++ b/src/webenginewidgets/api/qwebenginescriptcollection.cpp
@@ -172,32 +172,32 @@ QWebEngineScriptCollectionPrivate::QWebEngineScriptCollectionPrivate(QtWebEngine
 
 int QWebEngineScriptCollectionPrivate::count() const
 {
-    return m_scriptController->registeredScripts(m_contents).count();
+    return m_scriptController->registeredScripts(m_contents.data()).count();
 }
 
 bool QWebEngineScriptCollectionPrivate::contains(const QWebEngineScript &s) const
 {
-    return m_scriptController->containsUserScript(*s.d, m_contents);
+    return m_scriptController->containsUserScript(*s.d, m_contents.data());
 }
 
 void QWebEngineScriptCollectionPrivate::insert(const QWebEngineScript &script)
 {
     if (!script.d)
         return;
-    m_scriptController->addUserScript(*script.d, m_contents);
+    m_scriptController->addUserScript(*script.d, m_contents.data());
 }
 
 bool QWebEngineScriptCollectionPrivate::remove(const QWebEngineScript &script)
 {
     if (!script.d)
         return false;
-    return m_scriptController->removeUserScript(*script.d, m_contents);
+    return m_scriptController->removeUserScript(*script.d, m_contents.data());
 }
 
 QList<QWebEngineScript> QWebEngineScriptCollectionPrivate::toList(const QString &scriptName) const
 {
     QList<QWebEngineScript> ret;
-    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents))
+    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents.data()))
         if (scriptName.isNull() || scriptName == script.name())
             ret.append(QWebEngineScript(script));
     return ret;
@@ -205,7 +205,7 @@ QList<QWebEngineScript> QWebEngineScriptCollectionPrivate::toList(const QString
 
 QWebEngineScript QWebEngineScriptCollectionPrivate::find(const QString &name) const
 {
-    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents))
+    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents.data()))
         if (name == script.name())
             return QWebEngineScript(script);
     return QWebEngineScript();
@@ -213,12 +213,12 @@ QWebEngineScript QWebEngineScriptCollectionPrivate::find(const QString &name) co
 
 void QWebEngineScriptCollectionPrivate::clear()
 {
-    m_scriptController->clearAllScripts(m_contents);
+    m_scriptController->clearAllScripts(m_contents.data());
 }
 
 void QWebEngineScriptCollectionPrivate::reserve(int capacity)
 {
-    m_scriptController->reserve(m_contents, capacity);
+    m_scriptController->reserve(m_contents.data(), capacity);
 }
 
 void QWebEngineScriptCollectionPrivate::rebindToContents(QtWebEngineCore::WebContentsAdapter *page)
@@ -227,7 +227,7 @@ void QWebEngineScriptCollectionPrivate::rebindToContents(QtWebEngineCore::WebCon
     Q_ASSERT(page);
     Q_ASSERT(m_contents != page);
 
-    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents)) {
+    Q_FOREACH (const UserScript &script, m_scriptController->registeredScripts(m_contents.data())) {
         m_scriptController->addUserScript(script, page);
     }
     m_contents = page;
diff --git a/src/webenginewidgets/api/qwebenginescriptcollection_p.h b/src/webenginewidgets/api/qwebenginescriptcollection_p.h
index b5ae60a2cf08d3b96a5bf7c4a7073ebdfd7d19fe..931f6c0e83113b75921615ca3dc051fd60e74fa4 100644
--- a/src/webenginewidgets/api/qwebenginescriptcollection_p.h
+++ b/src/webenginewidgets/api/qwebenginescriptcollection_p.h
@@ -51,12 +51,13 @@
 #include "qtwebenginewidgetsglobal.h"
 
 #include "qwebenginescript.h"
+#include "web_contents_adapter.h"
 
+#include <QtCore/QExplicitlySharedDataPointer>
 #include <QtCore/QSet>
 
 namespace QtWebEngineCore {
 class UserScriptControllerHost;
-class WebContentsAdapter;
 } // namespace
 
 QT_BEGIN_NAMESPACE
@@ -78,7 +79,7 @@ public:
 
 private:
     QtWebEngineCore::UserScriptControllerHost *m_scriptController;
-    QtWebEngineCore::WebContentsAdapter *m_contents;
+    QExplicitlySharedDataPointer<QtWebEngineCore::WebContentsAdapter> m_contents;
 };
 
 QT_END_NAMESPACE