Set CppOwnership for managed QObjects returned by Q_INVOKABLES

I'm 99% sure of the recent crashes we've been seeing are double-frees,
the QCache one me and Duha encountered must be one. The QCache is in
charge of the one in ContentProvider, so it will sometimes try to delete
or access something already destroyed by the QML engine.

While I'm at it, I also made sure to check every other Q_INVOKABLE to
ensure we don't hit this elsewhere.

CCBUG: 502747
This commit is contained in:
Joshua Goins
2025-04-25 20:00:13 -04:00
parent 56472f6cfd
commit ccb162cfed
2 changed files with 13 additions and 4 deletions

View File

@@ -96,7 +96,9 @@ void RoomListModel::doResetModel()
NeoChatRoom *RoomListModel::roomAt(int row) const
{
return m_rooms.at(row);
auto room = m_rooms.at(row);
QQmlEngine::setObjectOwnership(room, QQmlEngine::CppOwnership);
return room;
}
void RoomListModel::doAddRoom(Room *r)
@@ -309,6 +311,7 @@ NeoChatRoom *RoomListModel::roomByAliasOrId(const QString &aliasOrId)
{
for (const auto &room : std::as_const(m_rooms)) {
if (room->aliases().contains(aliasOrId) || room->id() == aliasOrId) {
QQmlEngine::setObjectOwnership(room, QQmlEngine::CppOwnership);
return room;
}
}

View File

@@ -21,7 +21,9 @@ MessageContentModel *ContentProvider::contentModelForEvent(NeoChatRoom *room, co
}
if (!m_eventContentModels.contains(evtOrTxnId)) {
m_eventContentModels.insert(evtOrTxnId, new MessageContentModel(room, evtOrTxnId, isReply));
auto model = new MessageContentModel(room, evtOrTxnId, isReply);
QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership);
m_eventContentModels.insert(evtOrTxnId, model);
}
return m_eventContentModels.object(evtOrTxnId);
@@ -80,7 +82,9 @@ ThreadModel *ContentProvider::modelForThread(NeoChatRoom *room, const QString &t
}
if (!m_threadModels.contains(threadRootId)) {
m_threadModels.insert(threadRootId, new ThreadModel(threadRootId, room));
auto model = new ThreadModel(threadRootId, room);
QQmlEngine::setObjectOwnership(model, QQmlEngine::CppOwnership);
m_threadModels.insert(threadRootId, model);
}
return m_threadModels.object(threadRootId);
@@ -100,7 +104,9 @@ PollHandler *ContentProvider::handlerForPoll(NeoChatRoom *room, const QString &e
}
if (!m_pollHandlers.contains(eventId)) {
m_pollHandlers.insert(eventId, new PollHandler(room, eventId));
auto pollHandler = new PollHandler(room, eventId);
QQmlEngine::setObjectOwnership(pollHandler, QQmlEngine::CppOwnership);
m_pollHandlers.insert(eventId, pollHandler);
}
return m_pollHandlers.object(eventId);