@@ -379,45 +379,43 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
379
379
if (!env->Open (GetDataDir ()))
380
380
throw std::runtime_error (" CDB: Failed to open database environment." );
381
381
382
- strFile = strFilename;
383
- ++env->mapFileUseCount [strFile];
384
- pdb = env->mapDb [strFile];
382
+ pdb = env->mapDb [strFilename];
385
383
if (pdb == nullptr ) {
386
384
int ret;
387
- pdb = new Db (env->dbenv , 0 );
385
+ std::unique_ptr<Db> pdb_temp ( new Db (env->dbenv , 0 ) );
388
386
389
387
bool fMockDb = env->IsMock ();
390
388
if (fMockDb ) {
391
- DbMpoolFile* mpf = pdb ->get_mpf ();
389
+ DbMpoolFile* mpf = pdb_temp ->get_mpf ();
392
390
ret = mpf->set_flags (DB_MPOOL_NOFILE, 1 );
393
- if (ret != 0 )
394
- throw std::runtime_error (strprintf (" CDB: Failed to configure for no temp file backing for database %s" , strFile));
391
+ if (ret != 0 ) {
392
+ throw std::runtime_error (strprintf (" CDB: Failed to configure for no temp file backing for database %s" , strFilename));
393
+ }
395
394
}
396
395
397
- ret = pdb ->open (nullptr , // Txn pointer
398
- fMockDb ? nullptr : strFile .c_str (), // Filename
399
- fMockDb ? strFile .c_str () : " main" , // Logical db name
400
- DB_BTREE, // Database type
401
- nFlags, // Flags
396
+ ret = pdb_temp ->open (nullptr , // Txn pointer
397
+ fMockDb ? nullptr : strFilename .c_str (), // Filename
398
+ fMockDb ? strFilename .c_str () : " main" , // Logical db name
399
+ DB_BTREE, // Database type
400
+ nFlags, // Flags
402
401
0 );
403
402
404
403
if (ret != 0 ) {
405
- delete pdb;
406
- pdb = nullptr ;
407
- --env->mapFileUseCount [strFile];
408
- strFile = " " ;
409
404
throw std::runtime_error (strprintf (" CDB: Error %d, can't open database %s" , ret, strFilename));
410
405
}
411
406
407
+ pdb = pdb_temp.release ();
408
+ env->mapDb [strFilename] = pdb;
409
+
412
410
if (fCreate && !Exists (std::string (" version" ))) {
413
411
bool fTmp = fReadOnly ;
414
412
fReadOnly = false ;
415
413
WriteVersion (CLIENT_VERSION);
416
414
fReadOnly = fTmp ;
417
415
}
418
-
419
- env->mapDb [strFile] = pdb;
420
416
}
417
+ ++env->mapFileUseCount [strFilename];
418
+ strFile = strFilename;
421
419
}
422
420
}
423
421
0 commit comments