diff --git a/db/db_impl.cc b/db/db_impl.cc index b4f7a4c12..76382ecf4 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -3212,16 +3212,24 @@ Status DBImpl::NewIterators( const Snapshot* DBImpl::GetSnapshot() { int64_t unix_time = 0; env_->GetCurrentTime(&unix_time); // Ignore error + SnapshotImpl* s = new SnapshotImpl; InstrumentedMutexLock l(&mutex_); // returns null if the underlying memtable does not support snapshot. - if (!is_snapshot_supported_) return nullptr; - return snapshots_.New(versions_->LastSequence(), unix_time); + if (!is_snapshot_supported_) { + delete s; + return nullptr; + } + return snapshots_.New(s, versions_->LastSequence(), unix_time); } void DBImpl::ReleaseSnapshot(const Snapshot* s) { - InstrumentedMutexLock l(&mutex_); - snapshots_.Delete(reinterpret_cast(s)); + const SnapshotImpl* casted_s = reinterpret_cast(s); + { + InstrumentedMutexLock l(&mutex_); + snapshots_.Delete(casted_s); + } + delete casted_s; } // Convenience methods diff --git a/db/snapshot.h b/db/snapshot.h index c6852f5e5..b4d58fdf0 100644 --- a/db/snapshot.h +++ b/db/snapshot.h @@ -49,8 +49,8 @@ class SnapshotList { SnapshotImpl* oldest() const { assert(!empty()); return list_.next_; } SnapshotImpl* newest() const { assert(!empty()); return list_.prev_; } - const SnapshotImpl* New(SequenceNumber seq, uint64_t unix_time) { - SnapshotImpl* s = new SnapshotImpl; + const SnapshotImpl* New(SnapshotImpl* s, SequenceNumber seq, + uint64_t unix_time) { s->number_ = seq; s->unix_time_ = unix_time; s->list_ = this; @@ -62,12 +62,12 @@ class SnapshotList { return s; } + // Do not responsible to free the object. void Delete(const SnapshotImpl* s) { assert(s->list_ == this); s->prev_->next_ = s->next_; s->next_->prev_ = s->prev_; count_--; - delete s; } // retrieve all snapshot numbers. They are sorted in ascending order.