问题
Edit: FYI for future readers, this issue has been fixed as of version 2.3.606.0 of BitMiracle's LibTiff.NET.
I'm using BitMiracle's LibTiff.NET (version 2.3.605.0 and below) in my C# library (compiled at .NET 3.5 | x86) and keep getting this exception when I call ReadDirectory
: System.ObjectDisposedException: Cannot write to a closed TextWriter
I realize that this seems to indicate that I have already disposed of my image before making the call...but I have not specifically done so. Is this a bug in the library or am I really missing something here?
Here is my code:
public static bool IsTiffBiTonal(String tiffFilePath)
{
VerifyFileExistence(tiffFilePath);
using (Tiff tiff = Tiff.Open(tiffFilePath, "r"))
{
do
{
if (tiff.GetField(TiffTag.BITSPERSAMPLE)[0].ToInt() == 1)
{
continue;
}
return false;
}
while (tiff.ReadDirectory()); //Error occurs here
}
return true;
}
EDIT: Ok, I have more information after some further testing, this is only happening when I'm running my unit tests! Don't know why that would change anything though.
回答1:
Because of other threads talking about unit testing and getting this same error when trying to write to the console (ObjectDisposedException when outputting to console) I realized that the LibTiff.NET library was trying to write to the error console. After looking through the source code, I found that this code:
using (TextWriter stderr = Console.Error)
{
...
}
Because they were wrapping all of the writes to the error out in a using, it was disposing of the Console.Error object after the first write to the error out. This caused my error on the second time around (ReadDirectory does what calling Next on a linked list does). So I removed the using and the problem was fixed!
TextWriter stderr = Console.Error;
...
So, the lesson here: don't dispose of your standard outputs :)
I've asked another question regarding why they were ever allowed to dispose of the standard output in unit tests but not in other situations here: .NET - Why is disposing of standard output only allowed during unit tests?. If you have any answers to the question...please post it there.
来源:https://stackoverflow.com/questions/12114992/libtiff-net-readdirectory-is-giving-system-objectdisposedexception-only-during-u