#32 Problem when File constructor throws exception

closed-fixed
None
7
2012-12-02
2012-03-23
Bill
No

When the File constructor throws an exception either from within the constructor itself or from e.g. reader.ReadInfo(), then it is not disposed and leaves the IO.Stream open. This causes problems when using the same file in application code because the o/s thinks the file is still open.

The code needs a try/catch, something like this:

public File(
IInputStream stream
)
{
try
{
Initialize();

reader = new Reader(stream, this);

Reader.FileInfo info = reader.ReadInfo();
version = info.Version;
trailer = info.Trailer;
if (trailer.ContainsKey(PdfName.Encrypt)) // Encrypted file.
throw new NotImplementedException("Encrypted files are currently not supported.");

indirectObjects = new IndirectObjects(this, info.XrefEntries);
document = new Document(trailer[PdfName.Root]);
document.Configuration.XrefMode = (PdfName.XRef.Equals(trailer[PdfName.Type])
? Document.ConfigurationImpl.XRefModeEnum.Compressed
: Document.ConfigurationImpl.XRefModeEnum.Plain);
}
catch (Exception ex)
{
this.Dispose();
throw ex;
}
}

Discussion

    • priority: 5 --> 7
     
  • You are right: I'll fix it as soon as I can.

    thank you
    Stefano

     
    • assigned_to: nobody --> stechio
     
  • Since rev 85 [1] proper file disposal has been added to the code samples: you can get them checking out the project's SVN repo (they will be part of next 0.1.2 release). As you can see, responsibility for file disposal is kept on the user, as the library may receive an already-opened stream as input which is not entitled to close by itself.

    [1] http://clown.svn.sourceforge.net/viewvc/clown?view=revision&revision=85

     
    • status: open --> closed-fixed