Commit 0079a1c7 authored by Georgios Bitzes's avatar Georgios Bitzes
Browse files

Use unique_ptr for rocksdb::DB to prevent memory leak if StateMachine throws in constructor

parent e2d4aabc
Pipeline #477505 failed with stages
in 30 minutes and 4 seconds
......@@ -12,6 +12,9 @@ All notable changes to this project will be documented in this file.
From now on, a node which has been vetoed will abstain from starting election
rounds until it has received fresh heartbeats since receiving that veto.
### Fixed
- A couple of minor memory leaks.
## 0.3.1 (2018-08-03)
### Added
- Command `hclone` for creating identical copies of entire hashes.
......
......@@ -139,9 +139,11 @@ StateMachine::StateMachine(const std::string &f, bool write_ahead_log, bool bulk
options.allow_concurrent_memtable_write = false;
}
rocksdb::Status status = rocksdb::DB::Open(options, filename, &db);
rocksdb::DB *tmpdb = nullptr;
rocksdb::Status status = rocksdb::DB::Open(options, filename, &tmpdb);
if(!status.ok()) qdb_throw("Cannot open " << quotes(filename) << ":" << status.ToString());
db.reset(tmpdb);
ensureCompatibleFormat(!dirExists);
ensureBulkloadSanity(!dirExists);
ensureClockSanity(!dirExists);
......@@ -182,8 +184,7 @@ StateMachine::~StateMachine() {
if(db) {
qdb_info("Closing state machine " << quotes(filename));
delete db;
db = nullptr;
db.reset();
}
}
......@@ -594,7 +595,7 @@ rocksdb::Status StateMachine::lhlen(StagingArea &stagingArea, const std::string
}
rocksdb::Status StateMachine::rawGetAllVersions(const std::string &key, std::vector<rocksdb::KeyVersion> &versions) {
return rocksdb::GetAllKeyVersions(db, key, key, &versions);
return rocksdb::GetAllKeyVersions(db.get(), key, key, &versions);
}
rocksdb::Status StateMachine::rawScan(StagingArea &stagingArea, const std::string &key, size_t count, std::vector<std::string> &elements) {
......@@ -1470,7 +1471,7 @@ rocksdb::Status StateMachine::flushall(StagingArea &stagingArea) {
rocksdb::Status StateMachine::checkpoint(const std::string &path) {
rocksdb::Checkpoint *checkpoint = nullptr;
RETURN_ON_ERROR(rocksdb::Checkpoint::Create(db, &checkpoint));
RETURN_ON_ERROR(rocksdb::Checkpoint::Create(db.get(), &checkpoint));
rocksdb::Status st = checkpoint->CreateCheckpoint(path);
delete checkpoint;
......
......@@ -295,7 +295,7 @@ private:
std::mutex lastAppliedMtx;
std::mutex writeMtx;
rocksdb::DB* db = nullptr;
std::unique_ptr<rocksdb::DB> db;
std::unique_ptr<ConsistencyScanner> consistencyScanner;
......
......@@ -49,7 +49,7 @@ public:
if(readOnly) {
// Acquire snapshot.
snapshot.reset(new StateMachine::Snapshot(sm.db));
snapshot.reset(new StateMachine::Snapshot(sm.db.get()));
}
}
......@@ -65,7 +65,8 @@ public:
return rocksdb::Status::NotFound();
}
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db, rocksdb::ReadOptions(), slice, &value);
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db.get(),
rocksdb::ReadOptions(), slice, &value);
}
rocksdb::Status exists(const rocksdb::Slice &slice) {
......@@ -80,7 +81,7 @@ public:
}
rocksdb::PinnableSlice ignored;
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db, rocksdb::ReadOptions(), slice, &ignored);
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db.get(), rocksdb::ReadOptions(), slice, &ignored);
}
rocksdb::Status get(const rocksdb::Slice &slice, std::string &value) {
......@@ -92,7 +93,7 @@ public:
return stateMachine.db->Get(snapshot->opts(), slice, &value);
}
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db, rocksdb::ReadOptions(), slice, &value);
return writeBatchWithIndex.GetFromBatchAndDB(stateMachine.db.get(), rocksdb::ReadOptions(), slice, &value);
}
void put(const rocksdb::Slice &slice, const rocksdb::Slice &value) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment