@@ -4314,21 +4314,20 @@ util::Result<MigrationResult> MigrateLegacyToDescriptor(std::shared_ptr<CWallet>
4314
4314
std::set<fs::path> wallet_dirs;
4315
4315
if (success) {
4316
4316
// Migration successful, unload all wallets locally, then reload them.
4317
- // Reload the main wallet
4318
- wallet_dirs.insert (fs::PathFromString (local_wallet->GetDatabase ().Filename ()).parent_path ());
4319
- success = reload_wallet (local_wallet);
4317
+ // Note: We use a pointer to the shared_ptr to avoid increasing its reference count,
4318
+ // as 'reload_wallet' expects to be the sole owner (use_count == 1).
4319
+ for (std::shared_ptr<CWallet>* wallet_ptr : {&local_wallet, &res.watchonly_wallet , &res.solvables_wallet }) {
4320
+ if (success && *wallet_ptr) {
4321
+ std::shared_ptr<CWallet>& wallet = *wallet_ptr;
4322
+ // Save db path and reload wallet
4323
+ wallet_dirs.insert (fs::PathFromString (wallet->GetDatabase ().Filename ()).parent_path ());
4324
+ success = reload_wallet (wallet);
4325
+ }
4326
+ }
4327
+
4328
+ // Set main wallet
4320
4329
res.wallet = local_wallet;
4321
4330
res.wallet_name = wallet_name;
4322
- if (success && res.watchonly_wallet ) {
4323
- // Reload watchonly
4324
- wallet_dirs.insert (fs::PathFromString (res.watchonly_wallet ->GetDatabase ().Filename ()).parent_path ());
4325
- success = reload_wallet (res.watchonly_wallet );
4326
- }
4327
- if (success && res.solvables_wallet ) {
4328
- // Reload solvables
4329
- wallet_dirs.insert (fs::PathFromString (res.solvables_wallet ->GetDatabase ().Filename ()).parent_path ());
4330
- success = reload_wallet (res.solvables_wallet );
4331
- }
4332
4331
}
4333
4332
if (!success) {
4334
4333
// Migration failed, cleanup
0 commit comments