I have been using the DotSpatial library recently and noticed that my program was leaking memory quite severely. I've been using the VS memory diagnostic tools and managed to narrow the problem down the following (simplified) block of code:
using (var inMemoryStream = new MemoryStream())
using (var _writer = new BinaryWriter(inMemoryStream))
{
WriteHeader(_writer);
_writer.Close();
}
The BinaryWriter object maintains a reference to the inMemoryStream object, assigned to it's OutStream property. It seems that no matter what the disposal method (using / close / dispose) this memory stream never releases the memory it allocates for it's buffer.
I have managed to get around this by creating a class that inherits from BinaryWriter, overriding Close() and adding "OutStream = null" to the method, but this seems clunky.
Am I missing something? Is BinaryWriter not supposed to fully dispose of the stream that gets passed to it? It seems as if BinaryWriter.Close() does try to do something to this effect as viewing some of the properties of OutStream with intellisense shows an ObjectDisposed exception. I'm using .NET 4.5.2 if that makes a difference.
Thanks for the enlightenment in advance