-1

I am analyzing the code using Visual Studio tool and I get this message:

Object 'FileStream' can be disposed more than once in method 'BitmapFormat.WriteBitmap(byte[], int, int)'. To avoid generating a System.ObjectDisposedException you should not call Dispose more than one time on an object.

I tried to called disposed explicitly on the object but no avail.

This is the method:

Stream FileStream = null;
try
{
    FileStream = File.Open("finger.bmp", FileMode.Create, FileAccess.Write);
    BinaryWriter TmpBinaryWriter = new BinaryWriter(FileStream);
    TmpBinaryWriter.Write(StructToBytes(BmpHeader, 14));
    TmpBinaryWriter.Write(StructToBytes(BmpInfoHeader, Marshal.SizeOf(BmpInfoHeader)));

    //µ÷ÊÔ°åÐÅÏ¢
    for (ColorIndex = 0; ColorIndex < m_nColorTableEntries; ColorIndex++)
    {
        ColorMask[ColorIndex].redmask = (byte)ColorIndex;
        ColorMask[ColorIndex].greenmask = (byte)ColorIndex;
        ColorMask[ColorIndex].bluemask = (byte)ColorIndex;
        ColorMask[ColorIndex].rgbReserved = 0;

        TmpBinaryWriter.Write(StructToBytes(ColorMask[ColorIndex], Marshal.SizeOf(ColorMask[ColorIndex])));
    }

    //ͼƬÐýת£¬½â¾öÖ¸ÎÆÍ¼Æ¬µ¹Á¢µÄÎÊÌâ
    RotatePic(buffer, nWidth, nHeight, ref ResBuf);

    //дͼƬ
    //TmpBinaryWriter.Write(ResBuf);
    byte[] filter = null;
    if (w - nWidth > 0)
    {
        filter = new byte[w - nWidth];
    }
    for (int i = 0; i < nHeight; i++)
    {
        TmpBinaryWriter.Write(ResBuf, i * nWidth, nWidth);
        if (w - nWidth > 0)
        {
            TmpBinaryWriter.Write(ResBuf, 0, w - nWidth);
        }
    }

    TmpBinaryWriter.Close();
    FileStream.Close(); // <----- THE WARNING IS HERE
}
finally
{
    if (FileStream != null)
        FileStream.Dispose();
}

If I remove the try finally block, the same happens. Even if I use using statement.

How can I avoid it?

croxy
  • 4,082
  • 9
  • 28
  • 46
jstuardo
  • 3,901
  • 14
  • 61
  • 136
  • 4
    I'm voting to close this question as off-topic because this has so many [duplicates](https://stackoverflow.com/search?tab=relevance&q=%5bc%23%5d%20can%20be%20disposed%20more%20than%20once%20in%20method%20) I can't even pick one. – Zohar Peled Jan 15 '19 at 13:20
  • You ARE disposing twice. `FileStream.Close()` is effectley the same as `FileStream.Close()`. – Neil Jan 15 '19 at 13:20
  • `BinaryWriter` will dispose the underlying stream. Which is your `FileStream` – croxy Jan 15 '19 at 13:22
  • Possible duplicate of ["Object can be disposed of more than once" error](https://stackoverflow.com/questions/3982719/object-can-be-disposed-of-more-than-once-error) – croxy Jan 15 '19 at 13:23
  • Unrelated: Try to use the ["using"-statement](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) – Fildor Jan 15 '19 at 13:27

3 Answers3

0

Try to get rid off this line. You call dispose in finally block which will ALWAYS run. So you can delete this line.

Adam Jachocki
  • 1,897
  • 1
  • 12
  • 28
0

In the specific case of a FileStream, you don't need to dispose it to close the file, you only need to use the Close method.

Bruno Caceiro
  • 7,035
  • 1
  • 26
  • 45
0

This may happen because you potentially try to execute

FileStream.Close(), and

FileStream.Dispose()

There is no point using Dispose in your case, you can just use the Close method. The close method will execute the Dispose method with 'true' value.

Please watch this explenation of 'FileStream.Close()' method:

https://msdn.microsoft.com/en-us/library/aa328800(v=vs.71).aspx

This implementation of Close calls the Dispose method passing a true value.

DanielFr
  • 111
  • 1
  • 6