Skip to content

Commit

Permalink
core/proxywindow: fix ProxiedWindow proxy pointer after reload
Browse files Browse the repository at this point in the history
Previously was not updated after reload, causing QsWindowAttached to
use the old window pointer after it had been freed.
  • Loading branch information
outfoxxed committed Nov 29, 2024
1 parent 59298f6 commit 8882f7c
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/window/proxywindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ void ProxyWindowBase::connectWindow() {
generation->registerIncubationController(this->window->incubationController());
}

this->window->setProxy(this);

// clang-format off
QObject::connect(this->window, &QWindow::visibilityChanged, this, &ProxyWindowBase::visibleChanged);
QObject::connect(this->window, &QWindow::xChanged, this, &ProxyWindowBase::xChanged);
Expand Down
1 change: 1 addition & 0 deletions src/window/proxywindow.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@ class ProxiedWindow: public QQuickWindow {
, mProxy(proxy) {}

[[nodiscard]] ProxyWindowBase* proxy() const { return this->mProxy; }
void setProxy(ProxyWindowBase* proxy) { this->mProxy = proxy; }

signals:
void exposed();
Expand Down
28 changes: 28 additions & 0 deletions src/window/test/windowattached.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <qobject.h>
#include <qquickitem.h>
#include <qscopedpointer.h>
#include <qsignalspy.h>
#include <qtest.h>
#include <qtestcase.h>
Expand Down Expand Up @@ -36,6 +37,33 @@ void TestWindowAttachment::attachedBeforeReload() {
QCOMPARE(spy.length(), 1);
}

void TestWindowAttachment::earlyAttachReloaded() {
auto window1 = QScopedPointer(new ProxyWindowBase());
auto item1 = QScopedPointer(new QQuickItem());
item1->setParentItem(window1->contentItem());
window1->reload(nullptr);

auto window2 = ProxyWindowBase();
auto item2 = QQuickItem();
item2.setParentItem(window2.contentItem());

auto* attached = WindowInterface::qmlAttachedProperties(&item2);
QCOMPARE_NE(attached, nullptr);
QCOMPARE(attached->window(), nullptr);

auto spy = QSignalSpy(attached, &QsWindowAttached::windowChanged);
window2.reload(window1.get());

QCOMPARE(attached->window(), &window2);
QCOMPARE(spy.length(), 1);

item1.reset();
window1.reset();

QCOMPARE(attached->window(), &window2);
QCOMPARE(spy.length(), 1);
}

void TestWindowAttachment::owningWindowChanged() {
auto window1 = ProxyWindowBase();
auto window2 = ProxyWindowBase();
Expand Down
1 change: 1 addition & 0 deletions src/window/test/windowattached.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class TestWindowAttachment: public QObject {
private slots:
static void attachedAfterReload();
static void attachedBeforeReload();
static void earlyAttachReloaded();
static void owningWindowChanged();
static void nonItemParents();
};

0 comments on commit 8882f7c

Please sign in to comment.