pearcefleming / excellibrary

Automatically exported from code.google.com/p/excellibrary
0 stars 0 forks source link

Stream is not closing after an ArgumentOutOfRangeException exception #141

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1. Try to open a non-Excel file(e.g. a comma delimited file using xls 
extension.)

2. Handle the ArgumentOutOfRangeException

3. Try to open the same file by using Stream class

What is the expected output? What do you see instead?
The CompoundDocument.Open method throws an ArgumentOutOfRangeException but it 
do not release the loaded file in the Open method. Therefore, when trying to 
open the same file using Stream class, I see the exception:

IOException: The process cannot access the file 'filename' because it is being 
used by another process

I would expect a null value as output.

What version of the product are you using? On what operating system?
FileName: ExcelLibrary.dll   
Release Date: Jul 2011

Windows 7 SP1
Windows Server 2008 R2 SP1

georgeguitar.jorge@gmail.com

WORKAROUND:
        public static CompoundDocument Open(string file)
        {
            FileStream stream = File.Open(file, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
            BinaryReader reader = new BinaryReader(stream);
            FileHeader header = ReadHeader(reader);

            int sectorSize = (int)Math.Pow(2, header.SectorSizeInPot);

            CompoundDocument document = null;

            if (sectorSize > 0)
            {
                document = new CompoundDocument(stream, header);
                if (!document.CheckHeader()) return null;

                document.ReadDirectoryEntries();
            }
            else
            {
                stream.Close();
                stream.Dispose();

                reader.Close();
            }

            return document;
        }

Original issue reported on code.google.com by georgegu...@gmail.com on 10 Jan 2013 at 9:19

GoogleCodeExporter commented 8 years ago
Fix at r56

Original comment by jetcat on 1 Mar 2013 at 7:04