Skip to content

Commit 31462b9

Browse files
committed
core/reloader: fix UAF of old generation during scene destroy
1 parent 97bcdbe commit 31462b9

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/core/generation.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,17 @@ EngineGeneration::~EngineGeneration() {
4343
if (this->root != nullptr) this->root->deleteLater();
4444
}
4545

46+
void EngineGeneration::destroy() {
47+
if (this->root != nullptr) {
48+
QObject::connect(this->root, &QObject::destroyed, this, [this]() {
49+
delete this;
50+
});
51+
52+
this->root->deleteLater();
53+
this->root = nullptr;
54+
}
55+
}
56+
4657
void EngineGeneration::onReload(EngineGeneration* old) {
4758
if (old != nullptr) {
4859
// if the old generation holds the window incubation controller as the
@@ -61,12 +72,8 @@ void EngineGeneration::onReload(EngineGeneration* old) {
6172
emit this->reloadFinished();
6273

6374
if (old != nullptr) {
64-
QTimer::singleShot(0, [this, old]() {
65-
// The delete must happen in the next tick or you get segfaults,
66-
// seems to be deleteLater related.
67-
delete old;
68-
this->postReload();
69-
});
75+
old->destroy();
76+
QObject::connect(old, &QObject::destroyed, this, [this]() { this->postReload(); });
7077
} else {
7178
this->postReload();
7279
}

src/core/generation.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class EngineGeneration: public QObject {
4343
DelayedQmlIncubationController delayedIncubationController;
4444
bool reloadComplete = false;
4545

46+
void destroy();
47+
4648
signals:
4749
void filesChanged();
4850
void reloadFinished();

0 commit comments

Comments
 (0)