diff --git a/src/httprpc.cpp b/src/httprpc.cpp --- a/src/httprpc.cpp +++ b/src/httprpc.cpp @@ -62,7 +62,7 @@ }; /* Stored RPC timer interface (for unregistration) */ -static HTTPRPCTimerInterface *httpRPCTimerInterface = nullptr; +static std::unique_ptr httpRPCTimerInterface; static void JSONErrorReply(HTTPRequest *req, const UniValue &objError, const UniValue &id) { @@ -393,8 +393,9 @@ RegisterHTTPHandler("/wallet/", false, rpcFunction); #endif assert(EventBase()); - httpRPCTimerInterface = new HTTPRPCTimerInterface(EventBase()); - RPCSetTimerInterface(httpRPCTimerInterface); + httpRPCTimerInterface = std::unique_ptr( + new HTTPRPCTimerInterface(EventBase())); + RPCSetTimerInterface(httpRPCTimerInterface.get()); return true; } @@ -406,8 +407,7 @@ LogPrint(BCLog::RPC, "Stopping HTTP RPC server\n"); UnregisterHTTPHandler("/", true); if (httpRPCTimerInterface) { - RPCUnsetTimerInterface(httpRPCTimerInterface); - delete httpRPCTimerInterface; - httpRPCTimerInterface = nullptr; + RPCUnsetTimerInterface(httpRPCTimerInterface.get()); + httpRPCTimerInterface.reset(); } } diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -123,7 +123,8 @@ create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - CDBWrapper *dbw = new CDBWrapper(ph, (1 << 10), false, false, false); + std::unique_ptr dbw = std::unique_ptr( + new CDBWrapper(ph, (1 << 10), false, false, false)); char key = 'k'; uint256 in = InsecureRand256(); uint256 res; @@ -133,7 +134,7 @@ BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); // Call the destructor to free leveldb LOCK - delete dbw; + dbw.reset(); // Now, set up another wrapper that wants to obfuscate the same directory CDBWrapper odbw(ph, (1 << 10), false, false, true); @@ -165,7 +166,8 @@ create_directories(ph); // Set up a non-obfuscated wrapper to write some initial data. - CDBWrapper *dbw = new CDBWrapper(ph, (1 << 10), false, false, false); + std::unique_ptr dbw = std::unique_ptr( + new CDBWrapper(ph, (1 << 10), false, false, false)); char key = 'k'; uint256 in = InsecureRand256(); uint256 res; @@ -175,7 +177,7 @@ BOOST_CHECK_EQUAL(res.ToString(), in.ToString()); // Call the destructor to free leveldb LOCK - delete dbw; + dbw.reset(); // Simulate a -reindex by wiping the existing data store CDBWrapper odbw(ph, (1 << 10), false, true, true); diff --git a/src/wallet/db.h b/src/wallet/db.h --- a/src/wallet/db.h +++ b/src/wallet/db.h @@ -36,7 +36,7 @@ public: mutable CCriticalSection cs_db; - DbEnv *dbenv; + std::unique_ptr dbenv; std::map mapFileUseCount; std::map mapDb; diff --git a/src/wallet/db.cpp b/src/wallet/db.cpp --- a/src/wallet/db.cpp +++ b/src/wallet/db.cpp @@ -46,20 +46,17 @@ } 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(); } CDBEnv::~CDBEnv() { EnvShutdown(); - delete dbenv; - dbenv = nullptr; } void CDBEnv::Close() { @@ -149,7 +146,7 @@ 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; @@ -192,7 +189,7 @@ } 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 @@ -308,7 +305,7 @@ std::stringstream strDump; - Db db(dbenv, 0); + Db db(dbenv.get(), 0); int result = db.verify(strFile.c_str(), nullptr, &strDump, flags); if (result == DB_VERIFY_BAD) { LogPrintf("CDBEnv::Salvage: Database salvage found errors, all data " @@ -400,7 +397,7 @@ pdb = env->mapDb[strFilename]; if (pdb == nullptr) { - std::unique_ptr pdb_temp(new Db(env->dbenv, 0)); + std::unique_ptr pdb_temp(new Db(env->dbenv.get(), 0)); bool fMockDb = env->IsMock(); if (fMockDb) { @@ -514,7 +511,8 @@ { // surround usage of db with extra {} CDB db(dbw, "r"); - Db *pdbCopy = new Db(env->dbenv, 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 @@ -569,15 +567,14 @@ if (pdbCopy->close(0)) { fSuccess = false; } - 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/test/wallet_test_fixture.cpp b/src/wallet/test/wallet_test_fixture.cpp --- a/src/wallet/test/wallet_test_fixture.cpp +++ b/src/wallet/test/wallet_test_fixture.cpp @@ -10,7 +10,7 @@ #include "wallet/rpcdump.h" #include "wallet/wallet.h" -CWallet *pwalletMain; +std::unique_ptr pwalletMain; WalletTestingSetup::WalletTestingSetup(const std::string &chainName) : TestingSetup(chainName) { @@ -19,18 +19,18 @@ bool fFirstRun; std::unique_ptr dbw( new CWalletDBWrapper(&bitdb, "wallet_test.dat")); - pwalletMain = new CWallet(Params(), std::move(dbw)); + pwalletMain = + std::unique_ptr(new CWallet(Params(), std::move(dbw))); pwalletMain->LoadWallet(fFirstRun); - RegisterValidationInterface(pwalletMain); + RegisterValidationInterface(pwalletMain.get()); RegisterWalletRPCCommands(tableRPC); RegisterDumpRPCCommands(tableRPC); } WalletTestingSetup::~WalletTestingSetup() { - UnregisterValidationInterface(pwalletMain); - delete pwalletMain; - pwalletMain = nullptr; + UnregisterValidationInterface(pwalletMain.get()); + pwalletMain.reset(); bitdb.Flush(true); bitdb.Reset();