From 860e912583b48fef7dc5b0399c40a7696ccfec55 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Wed, 9 Aug 2017 15:01:54 +0200 Subject: Use unique_ptr for pwalletMain (CWallet) --- src/wallet/test/wallet_test_fixture.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp index e2f48c45ab..0dd21171ca 100644 --- a/src/wallet/test/wallet_test_fixture.cpp +++ b/src/wallet/test/wallet_test_fixture.cpp @@ -8,7 +8,7 @@ #include "wallet/db.h" #include "wallet/wallet.h" -CWallet *pwalletMain; +std::unique_ptr pwalletMain; WalletTestingSetup::WalletTestingSetup(const std::string& chainName): TestingSetup(chainName) @@ -17,18 +17,17 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName): bool fFirstRun; std::unique_ptr dbw(new CWalletDBWrapper(&bitdb, "wallet_test.dat")); - pwalletMain = new CWallet(std::move(dbw)); + pwalletMain = std::unique_ptr(new CWallet(std::move(dbw))); pwalletMain->LoadWallet(fFirstRun); - RegisterValidationInterface(pwalletMain); + RegisterValidationInterface(pwalletMain.get()); RegisterWalletRPCCommands(tableRPC); } WalletTestingSetup::~WalletTestingSetup() { - UnregisterValidationInterface(pwalletMain); - delete pwalletMain; - pwalletMain = nullptr; + UnregisterValidationInterface(pwalletMain.get()); + pwalletMain.reset(); bitdb.Flush(true); bitdb.Reset(); -- cgit v1.2.3 From 29ab96dbd2bc76152de6eff09755b1b66b29474d Mon Sep 17 00:00:00 2001 From: practicalswift Date: Wed, 9 Aug 2017 16:24:12 +0200 Subject: Use unique_ptr for dbenv (DbEnv) --- src/wallet/db.cpp | 19 ++++++++----------- src/wallet/db.h | 2 +- 2 files changed, 9 insertions(+), 12 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 5d48b01c2e..520c16d9c6 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -75,13 +75,12 @@ void CDBEnv::EnvShutdown() void CDBEnv::Reset() { - delete dbenv; - dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS); + dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS)); fDbEnvInit = false; fMockDb = false; } -CDBEnv::CDBEnv() : dbenv(nullptr) +CDBEnv::CDBEnv() { Reset(); } @@ -89,8 +88,6 @@ CDBEnv::CDBEnv() : dbenv(nullptr) CDBEnv::~CDBEnv() { EnvShutdown(); - delete dbenv; - dbenv = nullptr; } void CDBEnv::Close() @@ -182,7 +179,7 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type LOCK(cs_db); assert(mapFileUseCount.count(strFile) == 0); - Db db(dbenv, 0); + Db db(dbenv.get(), 0); int result = db.verify(strFile.c_str(), nullptr, nullptr, 0); if (result == 0) return VERIFY_OK; @@ -225,7 +222,7 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco } LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); - std::unique_ptr pdbCopy(new Db(bitdb.dbenv, 0)); + std::unique_ptr pdbCopy(new Db(bitdb.dbenv.get(), 0)); int ret = pdbCopy->open(nullptr, // Txn pointer filename.c_str(), // Filename "main", // Logical db name @@ -334,7 +331,7 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vectordbenv, 0); + Db* pdbCopy = new Db(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename @@ -577,10 +574,10 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip) delete pdbCopy; } if (fSuccess) { - Db dbA(env->dbenv, 0); + Db dbA(env->dbenv.get(), 0); if (dbA.remove(strFile.c_str(), nullptr, 0)) fSuccess = false; - Db dbB(env->dbenv, 0); + Db dbB(env->dbenv.get(), 0); if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0)) fSuccess = false; } diff --git a/src/wallet/db.h b/src/wallet/db.h index 14283ac8f8..e6d5a9f293 100644 --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -36,7 +36,7 @@ private: public: mutable CCriticalSection cs_db; - DbEnv *dbenv; + std::unique_ptr dbenv; std::map mapFileUseCount; std::map mapDb; -- cgit v1.2.3 From b45c597caadabbf10a1e34f2c9ee1d0916164eee Mon Sep 17 00:00:00 2001 From: practicalswift Date: Wed, 9 Aug 2017 16:30:57 +0200 Subject: Use unique_ptr for pdbCopy (Db) and fix potential memory leak --- src/wallet/db.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 520c16d9c6..cae8cd7dac 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -522,7 +522,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip) std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} CDB db(dbw, "r"); - Db* pdbCopy = new Db(env->dbenv.get(), 0); + std::unique_ptr pdbCopy = std::unique_ptr(new Db(env->dbenv.get(), 0)); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename @@ -571,7 +571,6 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip) } else { pdbCopy->close(0); } - delete pdbCopy; } if (fSuccess) { Db dbA(env->dbenv.get(), 0); -- cgit v1.2.3 From 3e09b390b411298b9da8cc3f92132bfad15ac156 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Tue, 15 Aug 2017 07:46:56 +0200 Subject: Use MakeUnique(...) instead of std::unique_ptr(new T(...)) --- src/wallet/db.cpp | 4 ++-- src/wallet/test/wallet_test_fixture.cpp | 2 +- src/wallet/wallet.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'src/wallet') diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index cae8cd7dac..0ae5f2b156 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -222,7 +222,7 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco } LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size()); - std::unique_ptr pdbCopy(new Db(bitdb.dbenv.get(), 0)); + std::unique_ptr pdbCopy = MakeUnique(bitdb.dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer filename.c_str(), // Filename "main", // Logical db name @@ -522,7 +522,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip) std::string strFileRes = strFile + ".rewrite"; { // surround usage of db with extra {} CDB db(dbw, "r"); - std::unique_ptr pdbCopy = std::unique_ptr(new Db(env->dbenv.get(), 0)); + std::unique_ptr pdbCopy = MakeUnique(env->dbenv.get(), 0); int ret = pdbCopy->open(nullptr, // Txn pointer strFileRes.c_str(), // Filename diff --git a/src/wallet/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp index 0dd21171ca..34f210fcd7 100644 --- a/src/wallet/test/wallet_test_fixture.cpp +++ b/src/wallet/test/wallet_test_fixture.cpp @@ -17,7 +17,7 @@ WalletTestingSetup::WalletTestingSetup(const std::string& chainName): bool fFirstRun; std::unique_ptr dbw(new CWalletDBWrapper(&bitdb, "wallet_test.dat")); - pwalletMain = std::unique_ptr(new CWallet(std::move(dbw))); + pwalletMain = MakeUnique(std::move(dbw)); pwalletMain->LoadWallet(fFirstRun); RegisterValidationInterface(pwalletMain.get()); diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index a357224a86..dceb818b50 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -3806,7 +3806,7 @@ CWallet* CWallet::CreateWalletFromFile(const std::string walletFile) uiInterface.InitMessage(_("Zapping all transactions from wallet...")); std::unique_ptr dbw(new CWalletDBWrapper(&bitdb, walletFile)); - std::unique_ptr tempWallet(new CWallet(std::move(dbw))); + std::unique_ptr tempWallet = MakeUnique(std::move(dbw)); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); if (nZapWalletRet != DB_LOAD_OK) { InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); -- cgit v1.2.3 From a357293c871d7eb9ccaf9fff61736248319f7a63 Mon Sep 17 00:00:00 2001 From: practicalswift Date: Wed, 18 Oct 2017 23:21:06 +0200 Subject: Use MakeUnique(...) --- src/wallet/db.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/wallet') diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp index 0ae5f2b156..ca8e680ff7 100644 --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -413,7 +413,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb pdb = env->mapDb[strFilename]; if (pdb == nullptr) { int ret; - std::unique_ptr pdb_temp(new Db(env->dbenv, 0)); + std::unique_ptr pdb_temp = MakeUnique(env->dbenv.get(), 0); bool fMockDb = env->IsMock(); if (fMockDb) { -- cgit v1.2.3