This isn’t a complete fix because there are still early returns with errors in the rescan logic below. I believe it is safe to move the RegisterValidationInterface call to the bottom of the function as I argued in #12647 because cs_main gets locked on line 4135.
jimpo’s right, f8c81def4f5f88074348890a613c1006824b1b5d [wallet] Fix potential memory leak in CreateWalletFromFile isn’t currently a complete fix for all memory leaks. Also, his suggestion to move RegisterValidationInterface and fix more leaks should be ok because of the cs_main lock. If you want to make that change, I would just add a comment above the RegisterValidationInterface call that says, “it is safe to register for notifications after rescanning (no notifications will be missed) because cs_main is held during the entire scan.” Only downside to this change is that if in the future we do optimize the rescan to not hold cs_main the entire time, it could introduce a bug where notifications are missed after the rescan completes.
Other options:
- Just drop this commit and deal with memory leaks in a separate PR.
- Just add a comment above the
temp_wallet.release() call in this commit noting that walletInstance will be leaked if there is an error and the code below returns nullptr.
- Get rid of
temp_wallet, and make walletInstance a unique ptr. Return walletInstance.release() on the last line of this function. Add UnregisterValidationInterface(walletInstance.get()) calls below where nullptr is returned.