Skip to content

Commit

Permalink
[wallet] Fix potential memory leak in CreateWalletFromFile
Browse files Browse the repository at this point in the history
Fix proposed by ryanofsky in
#12647 (comment)
  • Loading branch information
jnewbery committed May 15, 2018
1 parent 13da289 commit 59b87a2
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/wallet/wallet.cpp
Expand Up @@ -4012,7 +4012,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&

int64_t nStart = GetTimeMillis();
bool fFirstRun = true;
CWallet *walletInstance = new CWallet(name, WalletDatabase::Create(path));
// Make a temporary wallet unique pointer so memory doesn't get leaked if
// wallet creation fails.
auto temp_wallet = MakeUnique<CWallet>(name, WalletDatabase::Create(path));
CWallet* walletInstance = temp_wallet.get();
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
if (nLoadWalletRet != DBErrors::LOAD_OK)
{
Expand Down Expand Up @@ -4224,7 +4227,6 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
}

walletInstance->m_last_block_processed = chainActive.Tip();
RegisterValidationInterface(walletInstance);

if (chainActive.Tip() && chainActive.Tip() != pindexRescan)
{
Expand Down Expand Up @@ -4290,6 +4292,10 @@ CWallet* CWallet::CreateWalletFromFile(const std::string& name, const fs::path&
}
}
}

// Register with the validation interface. It's ok to do this after rescan since we're still holding cs_main.
RegisterValidationInterface(temp_wallet.release());

walletInstance->SetBroadcastTransactions(gArgs.GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));

{
Expand Down

0 comments on commit 59b87a2

Please sign in to comment.