Skip to content

Commit

Permalink
Fix shared pointer deletion before the slot was invoked
Browse files Browse the repository at this point in the history
Fixes: #9367
  • Loading branch information
TheOneRing committed Mar 1, 2022
1 parent 14146c8 commit 83d238f
Show file tree
Hide file tree
Showing 14 changed files with 30 additions and 26 deletions.
5 changes: 5 additions & 0 deletions changelog/unreleased/9367
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Bugfix: Fix crash on remove account

We fixed a potential reference to a deleted item, when an account was removed.

https://github.com/owncloud/client/issues/9367
4 changes: 2 additions & 2 deletions src/gui/accountmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void AccountManager::deleteAccount(AccountState *account)
auto settings = ConfigFile::settingsWithGroup(QLatin1String(accountsC));
settings->remove(account->account()->id());

emit accountRemoved(account);
emit accountRemoved(copy);
}

AccountPtr AccountManager::createAccount()
Expand All @@ -407,7 +407,7 @@ void AccountManager::shutdown()
{
const auto accounts = std::move(_accounts);
for (const auto &acc : accounts) {
emit accountRemoved(acc.data());
emit accountRemoved(acc);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/gui/accountmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public slots:

Q_SIGNALS:
void accountAdded(AccountState *account);
void accountRemoved(AccountState *account);
void accountRemoved(const AccountStatePtr &s);

private:
AccountManager() {}
Expand Down
6 changes: 3 additions & 3 deletions src/gui/activitywidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ ActivityWidget::ActivityWidget(QWidget *parent)
connect(_model, &ActivityListModel::activityJobStatusCode,
this, &ActivityWidget::slotAccountActivityStatus);

connect(AccountManager::instance(), &AccountManager::accountRemoved, this, [this](AccountState *ast) {
connect(AccountManager::instance(), &AccountManager::accountRemoved, this, [this](const AccountStatePtr &ast) {
if (_accountsWithoutActivities.remove(ast->account()->displayName())) {
showLabels();
}
Expand Down Expand Up @@ -139,7 +139,7 @@ void ActivityWidget::slotRefreshNotifications(AccountState *ptr)
}
}

void ActivityWidget::slotRemoveAccount(AccountState *ptr)
void ActivityWidget::slotRemoveAccount(const AccountStatePtr &ptr)
{
_model->slotRemoveAccount(ptr);
}
Expand Down Expand Up @@ -556,7 +556,7 @@ void ActivitySettings::slotShowIssuesTab()
_tab->setCurrentIndex(_syncIssueTabId);
}

void ActivitySettings::slotRemoveAccount(AccountState *ptr)
void ActivitySettings::slotRemoveAccount(const AccountStatePtr &ptr)
{
_activityWidget->slotRemoveAccount(ptr);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/activitywidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ActivityWidget : public QWidget
public slots:
void slotRefreshActivities(AccountState *ptr);
void slotRefreshNotifications(AccountState *ptr);
void slotRemoveAccount(AccountState *ptr);
void slotRemoveAccount(const AccountStatePtr &ptr);
void slotAccountActivityStatus(AccountState *ast, int statusCode);
void slotRequestCleanupAndBlacklist(const Activity &blacklistActivity);

Expand Down Expand Up @@ -134,7 +134,7 @@ class ActivitySettings : public QWidget

public slots:
void slotRefresh(AccountState *ptr);
void slotRemoveAccount(AccountState *ptr);
void slotRemoveAccount(const AccountStatePtr &ptr);

void setNotificationRefreshInterval(std::chrono::milliseconds interval);

Expand Down
6 changes: 3 additions & 3 deletions src/gui/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,16 @@ Application::~Application()
AccountManager::instance()->shutdown();
}

void Application::slotAccountStateRemoved(AccountState *accountState)
void Application::slotAccountStateRemoved(const AccountStatePtr &accountState)
{
if (_gui) {
disconnect(accountState, &AccountState::stateChanged,
disconnect(accountState.data(), &AccountState::stateChanged,
_gui.data(), &ownCloudGui::slotAccountStateChanged);
disconnect(accountState->account().data(), &Account::serverVersionChanged,
_gui.data(), &ownCloudGui::slotTrayMessageIfServerUnsupported);
}
if (_folderManager) {
disconnect(accountState, &AccountState::stateChanged,
disconnect(accountState.data(), &AccountState::stateChanged,
_folderManager.data(), &FolderMan::slotAccountStateChanged);
disconnect(accountState->account().data(), &Account::serverVersionChanged,
_folderManager.data(), &FolderMan::slotServerVersionChanged);
Expand Down
2 changes: 1 addition & 1 deletion src/gui/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ protected slots:
void slotUseMonoIconsChanged(bool);
void slotCleanup();
void slotAccountStateAdded(AccountState *accountState);
void slotAccountStateRemoved(AccountState *accountState);
void slotAccountStateRemoved(const AccountStatePtr &accountState);
void slotSystemOnlineConfigurationChanged(QNetworkConfiguration);

private:
Expand Down
4 changes: 2 additions & 2 deletions src/gui/folderman.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -913,13 +913,13 @@ void FolderMan::slotEtagPollTimerTimeout()
}
}

void FolderMan::slotRemoveFoldersForAccount(AccountState *accountState)
void FolderMan::slotRemoveFoldersForAccount(const AccountStatePtr &accountState)
{
QList<Folder *> foldersToRemove;
// reserve a magic number
foldersToRemove.reserve(16);
for (auto *folder : qAsConst(_folderMap)) {
if (folder->accountState() == accountState) {
if (folder->accountState() == accountState.data()) {
foldersToRemove.append(folder);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/folderman.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ private slots:
void slotStartScheduledFolderSync();
void slotEtagPollTimerTimeout();

void slotRemoveFoldersForAccount(AccountState *accountState);
void slotRemoveFoldersForAccount(const AccountStatePtr &accountState);

// Wraps the Folder::syncStateChange() signal into the
// FolderMan::folderSyncStateChange(Folder*) signal.
Expand Down
8 changes: 4 additions & 4 deletions src/gui/models/activitylistmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,9 @@ void ActivityListModel::slotRefreshActivity(AccountState *ast)
startFetchJob(ast);
}

void ActivityListModel::slotRemoveAccount(AccountState *ast)
void ActivityListModel::slotRemoveAccount(const AccountStatePtr &ast)
{
if (_activityLists.contains(ast)) {
if (_activityLists.contains(ast.data())) {
const auto accountToRemove = ast->account()->uuid();

QMutableListIterator<Activity> it(_finalList);
Expand All @@ -287,8 +287,8 @@ void ActivityListModel::slotRemoveAccount(AccountState *ast)
++i;
}
}
_activityLists.remove(ast);
_currentlyFetching.remove(ast);
_activityLists.remove(ast.data());
_currentlyFetching.remove(ast.data());
}
}
}
5 changes: 2 additions & 3 deletions src/gui/models/activitylistmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <QtCore>

#include "accountstate.h"
#include "activitydata.h"

class QJsonDocument;
Expand All @@ -25,8 +26,6 @@ namespace OCC {

Q_DECLARE_LOGGING_CATEGORY(lcActivity)

class AccountState;

/**
* @brief The ActivityListModel
* @ingroup gui
Expand Down Expand Up @@ -63,7 +62,7 @@ class ActivityListModel : public QAbstractTableModel

public slots:
void slotRefreshActivity(AccountState *ast);
void slotRemoveAccount(AccountState *ast);
void slotRemoveAccount(const AccountStatePtr &ast);

signals:
void activityJobStatusCode(AccountState *ast, int statusCode);
Expand Down
4 changes: 2 additions & 2 deletions src/gui/settingsdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,14 +423,14 @@ void SettingsDialog::slotAccountDisplayNameChanged()
}
}

void SettingsDialog::accountRemoved(AccountState *s)
void SettingsDialog::accountRemoved(const AccountStatePtr &s)
{
for (auto it = _actionGroupWidgets.begin(); it != _actionGroupWidgets.end(); ++it) {
auto as = qobject_cast<AccountSettings *>(*it);
if (!as) {
continue;
}
if (as->accountsState() == s) {
if (as->accountsState() == s.data()) {
_ui->toolBar->removeAction(it.key());

if (_ui->stack->currentWidget() == it.value()) {
Expand Down
2 changes: 1 addition & 1 deletion src/gui/settingsdialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public slots:

private slots:
void accountAdded(AccountState *);
void accountRemoved(AccountState *);
void accountRemoved(const AccountStatePtr &);

private:
void customizeStyle();
Expand Down
2 changes: 1 addition & 1 deletion test/modeltests/testactivitymodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ private Q_SLOTS:
Activity { Activity::ActivityType, 2, acc1, "test", "test", "foo.cpp", QUrl::fromUserInput("https://owncloud.com"), QDateTime::currentDateTime() },
Activity { Activity::ActivityType, 4, acc2, "test", "test", "foo.cpp", QUrl::fromUserInput("https://owncloud.com"), QDateTime::currentDateTime() },
});
model->slotRemoveAccount(AccountManager::instance()->accounts().first().data());
model->slotRemoveAccount(AccountManager::instance()->accounts().first());
}
};
}
Expand Down

0 comments on commit 83d238f

Please sign in to comment.