From e86d71b749c08bde6002b9aa2baee824975a518a Mon Sep 17 00:00:00 2001 From: furszy Date: Wed, 28 May 2025 06:17:54 -0400 Subject: [PATCH] wallet: refactor, dedup wallet re-loading code --- src/wallet/wallet.cpp | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 6542abc23df..fb5c74c9f7f 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4314,21 +4314,20 @@ util::Result MigrateLegacyToDescriptor(std::shared_ptr std::set wallet_dirs; if (success) { // Migration successful, unload all wallets locally, then reload them. - // Reload the main wallet - wallet_dirs.insert(fs::PathFromString(local_wallet->GetDatabase().Filename()).parent_path()); - success = reload_wallet(local_wallet); + // Note: We use a pointer to the shared_ptr to avoid increasing its reference count, + // as 'reload_wallet' expects to be the sole owner (use_count == 1). + for (std::shared_ptr* wallet_ptr : {&local_wallet, &res.watchonly_wallet, &res.solvables_wallet}) { + if (success && *wallet_ptr) { + std::shared_ptr& wallet = *wallet_ptr; + // Save db path and reload wallet + wallet_dirs.insert(fs::PathFromString(wallet->GetDatabase().Filename()).parent_path()); + success = reload_wallet(wallet); + } + } + + // Set main wallet res.wallet = local_wallet; res.wallet_name = wallet_name; - if (success && res.watchonly_wallet) { - // Reload watchonly - wallet_dirs.insert(fs::PathFromString(res.watchonly_wallet->GetDatabase().Filename()).parent_path()); - success = reload_wallet(res.watchonly_wallet); - } - if (success && res.solvables_wallet) { - // Reload solvables - wallet_dirs.insert(fs::PathFromString(res.solvables_wallet->GetDatabase().Filename()).parent_path()); - success = reload_wallet(res.solvables_wallet); - } } if (!success) { // Migration failed, cleanup