From a4dfd6c823b627c98c64fae42d4016d325d14ae4 Mon Sep 17 00:00:00 2001
From: Guillaume Roguez <guillaume.roguez@savoirfairelinux.com>
Date: Thu, 14 Dec 2017 21:59:51 -0500
Subject: [PATCH] fix crash when last account is destroyed
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

When deleting the last account, method ProfileModelPrivate::slotAccountRemoved
destroy the account then save the profile.
This last triggers NewAccountModelPimpl::slotProfileUpdated().
But this one access to an empty account array (as all account are destroyed).

Re-order profile-save/account delete to solve the issue (save first, destroy after).

Change-Id: I427a51c3d1f97d0127567531616f360df45e4652
Reviewed-by: Nicolas Jäger <nicolas.jager@savoirfairelinux.com>
---
 src/profilemodel.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/profilemodel.cpp b/src/profilemodel.cpp
index 3ddd640c..837916f8 100644
--- a/src/profilemodel.cpp
+++ b/src/profilemodel.cpp
@@ -254,10 +254,10 @@ void ProfileModelPrivate::slotAccountRemoved(Account* a)
             const int accIdx = n->m_Index;
             q_ptr->beginRemoveRows(profIdx, accIdx, accIdx);
             n->parent->children.removeAt(accIdx);
-            n->parent->m_uContent.m_pProfile->removeAccount(n->m_uContent.m_pAccount);
             for (int i = accIdx; i < n->parent->children.size(); i++)
                 n->parent->children[i]->m_Index--;
             n->parent->m_uContent.m_pProfile->save();
+            n->parent->m_uContent.m_pProfile->removeAccount(n->m_uContent.m_pAccount);
             delete n;
             q_ptr->endRemoveRows();
         }
-- 
GitLab