From fb63ac56420e678334b1edb368386e09166002ce Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sun, 1 Sep 2024 12:10:42 -0400 Subject: [PATCH] Fix a crash when sitting around with no room open This doesn't check if m_room is null before accessing it's connection. You can easily reproduce this by sitting around leaving NeoChat open without entering a room. --- src/models/messagecontentmodel.cpp | 27 +++++++++++++-------------- src/models/messageeventmodel.cpp | 26 ++++++++++++-------------- 2 files changed, 25 insertions(+), 28 deletions(-) diff --git a/src/models/messagecontentmodel.cpp b/src/models/messagecontentmodel.cpp index 643c37f4f..855a6e151 100644 --- a/src/models/messagecontentmodel.cpp +++ b/src/models/messagecontentmodel.cpp @@ -578,22 +578,21 @@ MessageComponent MessageContentModel::linkPreviewComponent(const QUrl &link) } if (linkPreviewer->loaded()) { return MessageComponent{MessageComponentType::LinkPreview, QString(), {{"link"_ls, link}}}; - } else { - connect(linkPreviewer, &LinkPreviewer::loadedChanged, [this, link]() { - const auto linkPreviewer = dynamic_cast(m_room->connection())->previewerForLink(link); - if (linkPreviewer != nullptr && linkPreviewer->loaded()) { - for (auto &component : m_components) { - if (component.attributes["link"_ls].toUrl() == link) { - // HACK: Because DelegateChooser can't switch the delegate on dataChanged it has to think there is a new delegate. - beginResetModel(); - component.type = MessageComponentType::LinkPreview; - endResetModel(); - } + } + connect(linkPreviewer, &LinkPreviewer::loadedChanged, this, [this, link]() { + const auto linkPreviewer = dynamic_cast(m_room->connection())->previewerForLink(link); + if (linkPreviewer != nullptr && linkPreviewer->loaded()) { + for (auto &component : m_components) { + if (component.attributes["link"_ls].toUrl() == link) { + // HACK: Because DelegateChooser can't switch the delegate on dataChanged it has to think there is a new delegate. + beginResetModel(); + component.type = MessageComponentType::LinkPreview; + endResetModel(); } } - }); - return MessageComponent{MessageComponentType::LinkPreviewLoad, QString(), {{"link"_ls, link}}}; - } + } + }); + return MessageComponent{MessageComponentType::LinkPreviewLoad, QString(), {{"link"_ls, link}}}; } QList MessageContentModel::addLinkPreviews(QList inputComponents) diff --git a/src/models/messageeventmodel.cpp b/src/models/messageeventmodel.cpp index 6d8ace137..57b72f64d 100644 --- a/src/models/messageeventmodel.cpp +++ b/src/models/messageeventmodel.cpp @@ -84,21 +84,19 @@ void MessageEventModel::setRoom(NeoChatRoom *room) return; } - if (m_currentRoom) { - // HACK: Reset the model to a null room first to make sure QML dismantles - // last room's objects before the room is actually changed - beginResetModel(); - m_currentRoom->disconnect(this); - m_currentRoom = nullptr; - endResetModel(); + // HACK: Reset the model to a null room first to make sure QML dismantles + // last room's objects before the room is actually changed + beginResetModel(); + m_currentRoom->disconnect(this); + m_currentRoom = nullptr; + endResetModel(); - // Don't clear the member objects until the model has been fully reset and all - // refs cleared. - m_memberObjects.clear(); - m_contentModels.clear(); - m_reactionModels.clear(); - m_readMarkerModels.clear(); - } + // Don't clear the member objects until the model has been fully reset and all + // refs cleared. + m_memberObjects.clear(); + m_contentModels.clear(); + m_reactionModels.clear(); + m_readMarkerModels.clear(); beginResetModel(); m_currentRoom = room;