Skip to content

Commit 5b31813

Browse files
practicalswiftfurszy
authored andcommitted
Use unique_ptr for dbenv (DbEnv)
1 parent a1bef4f commit 5b31813

File tree

2 files changed

+15
-18
lines changed

2 files changed

+15
-18
lines changed

src/wallet/db.cpp

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -77,22 +77,19 @@ void CDBEnv::EnvShutdown()
7777

7878
void CDBEnv::Reset()
7979
{
80-
delete dbenv;
81-
dbenv = new DbEnv(DB_CXX_NO_EXCEPTIONS);
80+
dbenv.reset(new DbEnv(DB_CXX_NO_EXCEPTIONS));
8281
fDbEnvInit = false;
8382
fMockDb = false;
8483
}
8584

86-
CDBEnv::CDBEnv() : dbenv(NULL)
85+
CDBEnv::CDBEnv()
8786
{
8887
Reset();
8988
}
9089

9190
CDBEnv::~CDBEnv()
9291
{
9392
EnvShutdown();
94-
delete dbenv;
95-
dbenv = NULL;
9693
}
9794

9895
void CDBEnv::Close()
@@ -184,8 +181,8 @@ CDBEnv::VerifyResult CDBEnv::Verify(const std::string& strFile, recoverFunc_type
184181
LOCK(cs_db);
185182
assert(mapFileUseCount.count(strFile) == 0);
186183

187-
Db db(dbenv, 0);
188-
int result = db.verify(strFile.c_str(), NULL, NULL, 0);
184+
Db db(dbenv.get(), 0);
185+
int result = db.verify(strFile.c_str(), nullptr, nullptr, 0);
189186
if (result == 0)
190187
return VERIFY_OK;
191188
else if (recoverFunc == NULL)
@@ -225,8 +222,8 @@ bool CDB::Recover(const std::string& filename, void *callbackDataIn, bool (*reco
225222
}
226223
LogPrintf("Salvage(aggressive) found %u records\n", salvagedData.size());
227224

228-
std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv, 0));
229-
int ret = pdbCopy->open(NULL, // Txn pointer
225+
std::unique_ptr<Db> pdbCopy(new Db(bitdb.dbenv.get(), 0));
226+
int ret = pdbCopy->open(nullptr, // Txn pointer
230227
filename.c_str(), // Filename
231228
"main", // Logical db name
232229
DB_BTREE, // Database type
@@ -326,8 +323,8 @@ bool CDBEnv::Salvage(const std::string& strFile, bool fAggressive, std::vector<C
326323

327324
std::stringstream strDump;
328325

329-
Db db(dbenv, 0);
330-
int result = db.verify(strFile.c_str(), NULL, &strDump, flags);
326+
Db db(dbenv.get(), 0);
327+
int result = db.verify(strFile.c_str(), nullptr, &strDump, flags);
331328
if (result == DB_VERIFY_BAD) {
332329
LogPrintf("CDBEnv::Salvage : Database salvage found errors, all data may not be recoverable.\n");
333330
if (!fAggressive) {
@@ -408,7 +405,7 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
408405
pdb = env->mapDb[strFilename];
409406
if (pdb == nullptr) {
410407
int ret;
411-
std::unique_ptr<Db> pdb_temp(new Db(env->dbenv, 0));
408+
std::unique_ptr<Db> pdb_temp(new Db(env->dbenv.get(), 0));
412409

413410
bool fMockDb = env->IsMock();
414411
if (fMockDb) {
@@ -517,7 +514,7 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
517514
std::string strFileRes = strFile + ".rewrite";
518515
{ // surround usage of db with extra {}
519516
CDB db(dbw, "r");
520-
Db* pdbCopy = new Db(env->dbenv, 0);
517+
Db* pdbCopy = new Db(env->dbenv.get(), 0);
521518

522519
int ret = pdbCopy->open(NULL, // Txn pointer
523520
strFileRes.c_str(), // Filename
@@ -569,11 +566,11 @@ bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
569566
delete pdbCopy;
570567
}
571568
if (fSuccess) {
572-
Db dbA(env->dbenv, 0);
573-
if (dbA.remove(strFile.c_str(), NULL, 0))
569+
Db dbA(env->dbenv.get(), 0);
570+
if (dbA.remove(strFile.c_str(), nullptr, 0))
574571
fSuccess = false;
575-
Db dbB(env->dbenv, 0);
576-
if (dbB.rename(strFileRes.c_str(), NULL, strFile.c_str(), 0))
572+
Db dbB(env->dbenv.get(), 0);
573+
if (dbB.rename(strFileRes.c_str(), nullptr, strFile.c_str(), 0))
577574
fSuccess = false;
578575
}
579576
if (!fSuccess)

src/wallet/db.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class CDBEnv
3737

3838
public:
3939
mutable RecursiveMutex cs_db;
40-
DbEnv *dbenv;
40+
std::unique_ptr<DbEnv> dbenv;
4141
std::map<std::string, int> mapFileUseCount;
4242
std::map<std::string, Db*> mapDb;
4343

0 commit comments

Comments
 (0)