From 514793d7a0a8d057717d2a734de82a1d030a81d3 Mon Sep 17 00:00:00 2001 From: John Ralls Date: Mon, 19 Aug 2024 12:39:07 -0700 Subject: [PATCH] Bug 799389 - Crash when removing an account Destroying the split vector from front->back crashes halfway through because the iterators aren't updated as we remove items from the vector. Iterating in reverse works because the remaining elements aren't moved as we delete. --- libgnucash/engine/Account.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/libgnucash/engine/Account.cpp b/libgnucash/engine/Account.cpp index 5401b7a6fe6..fb514ba6931 100644 --- a/libgnucash/engine/Account.cpp +++ b/libgnucash/engine/Account.cpp @@ -1546,8 +1546,9 @@ xaccAccountCommitEdit (Account *acc) themselves will be destroyed by the transaction code */ if (!qof_book_shutting_down(book)) { - for (auto s : priv->splits) - xaccSplitDestroy (s); + // We need to delete in reverse order so that the vector's iterators aren't invalidated. + for_each(priv->splits.rbegin(), priv->splits.rend(), [](Split *s) { + xaccSplitDestroy (s); }); } else {