Skip to content

Commit

Permalink
Merge pull request planetarium#870 from earlbread/fix-iterator-dispose
Browse files Browse the repository at this point in the history
Dispose iterator after use
  • Loading branch information
earlbread authored May 20, 2020
2 parents 272674d + fc6a98f commit c08e9e1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Libplanet changelog
===================

Version 0.9.2
-------------

To be released.

- (Libplanet.RocksDBStore) Fixed a memory leak bug in `RocksDBStore`. [[#870]]

[#870]: https://github.com/planetarium/libplanet/pull/870


Version 0.9.1
-------------

Expand Down
14 changes: 7 additions & 7 deletions Libplanet.RocksDBStore/RocksDBStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -708,11 +708,8 @@ public override IEnumerable<Tuple<HashDigest<SHA256>, long>> IterateStateReferen
byte[] keyBytes = RocksDBStoreBitConverter.GetBytes(key);
byte[] prefix = StateRefKeyPrefix.Concat(keyBytes).ToArray();

ColumnFamilyHandle cf = GetColumnFamily(_stateRefDb, chainId);
Iterator it = _stateRefDb.NewIterator(cf);

return IterateStateReferences(
prefix, it, highestIndex.Value, lowestIndex.Value, limit.Value);
chainId, prefix, highestIndex.Value, lowestIndex.Value, limit.Value);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -773,7 +770,7 @@ public override void ForkStateReferences<T>(
_stateRefDb.Put(key, it.Value(), destCf);
}

Iterator destIt = _stateRefDb.NewIterator(destCf);
using Iterator destIt = _stateRefDb.NewIterator(destCf);

destIt.Seek(prefix);

Expand Down Expand Up @@ -883,12 +880,15 @@ private void DeleteBlockStates(
}

private IEnumerable<Tuple<HashDigest<SHA256>, long>> IterateStateReferences(
Guid chainId,
byte[] prefix,
Iterator it,
long highestIndex,
long lowestIndex,
int limit)
{
ColumnFamilyHandle cf = GetColumnFamily(_stateRefDb, chainId);
using Iterator it = _stateRefDb.NewIterator(cf);

// FIXME: We need to change the state reference to be ordered by reverse byte-wise
// and use the Seek function.
it.SeekToLast();
Expand Down Expand Up @@ -961,7 +961,7 @@ private byte[] StateRefKey(string stateKey, long blockIndex)
private IEnumerable<Iterator> IterateDb(RocksDb db, byte[] prefix, Guid? chainId = null)
{
ColumnFamilyHandle cf = GetColumnFamily(db, chainId);
Iterator it = db.NewIterator(cf);
using Iterator it = db.NewIterator(cf);
for (it.Seek(prefix); it.Valid() && it.Key().StartsWith(prefix); it.Next())
{
yield return it;
Expand Down
2 changes: 1 addition & 1 deletion Libplanet/Libplanet.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<PackageId>Libplanet</PackageId>
<Title>Libplanet</Title>
<VersionPrefix>0.9.1</VersionPrefix>
<VersionPrefix>0.9.2</VersionPrefix>
<!-- Note: don't be confused by the word "prefix" here. It's merely a
version without suffix like "-dev.123". See the following examples:
Version: 1.2.3-dev.456
Expand Down

0 comments on commit c08e9e1

Please sign in to comment.