Dispose enumerators after manually iterating over elements.

Since the IEnumerator<T> interface implements IDisposable, we should
make sure to dispose of the enumerator when manually iterating over the
elements.
This commit is contained in:
Kenneth Hsu
2017-10-07 12:27:11 -07:00
parent 38a8d11266
commit 604b6e4337
2 changed files with 20 additions and 16 deletions
@@ -558,17 +558,19 @@ ORDER BY
"
,blocksize, blockhashlength);
var en = blocklist.GetEnumerator();
foreach(var r in cmd.ExecuteReaderEnumerable(query, hash, length))
using (var en = blocklist.GetEnumerator())
{
if (!en.MoveNext())
throw new Exception(string.Format("Too few entries in source blocklist with hash {0}", hash));
if (en.Current != r.GetString(0))
throw new Exception(string.Format("Mismatch in blocklist with hash {0}", hash));
}
foreach (var r in cmd.ExecuteReaderEnumerable(query, hash, length))
{
if (!en.MoveNext())
throw new Exception(string.Format("Too few entries in source blocklist with hash {0}", hash));
if (en.Current != r.GetString(0))
throw new Exception(string.Format("Mismatch in blocklist with hash {0}", hash));
}
if (en.MoveNext())
throw new Exception(string.Format("Too many source blocklist entries in {0}", hash));
if (en.MoveNext())
throw new Exception(string.Format("Too many source blocklist entries in {0}", hash));
}
}
}
}
@@ -83,15 +83,17 @@ namespace Duplicati.Library.Main.Volumes
if (blocklisthashes != null)
{
//Slightly akward, but we avoid writing if there are no entries
var en = blocklisthashes.GetEnumerator();
if (en.MoveNext() && !string.IsNullOrEmpty(en.Current))
using (var en = blocklisthashes.GetEnumerator())
{
m_writer.WritePropertyName("blocklists");
m_writer.WriteStartArray();
m_writer.WriteValue(en.Current);
while (en.MoveNext())
if (en.MoveNext() && !string.IsNullOrEmpty(en.Current))
{
m_writer.WritePropertyName("blocklists");
m_writer.WriteStartArray();
m_writer.WriteValue(en.Current);
m_writer.WriteEndArray();
while (en.MoveNext())
m_writer.WriteValue(en.Current);
m_writer.WriteEndArray();
}
}
}
else if (!string.IsNullOrWhiteSpace(blockhash))