Skip to content

Ensuring file handles are closed properly

Sambit Kumar Dash edited this page Jul 23, 2017 · 7 revisions

PDF files are stream based. Most of the contents for documents reside in streams. Streams can be:

  1. Compressed
  2. Encrypted
  3. Can be externalized to the PDF files
  4. Can be used to store PDF objects

This creates the need for accessing them several times in the lifetime of a PDF file access. This means you cannot really parse the PDF files in one go and load all contents into memory. When you try to access a file on-demand multiple number of times you tend to risk possibility of leaving out open handles for files. This can be a problem for non-tolerant OS like Window that locks files before access. However, you may not have a Windows OS with you to test it effectively. Depending on AppVeyor, post every build is a bit of a pain considering you have to check-in before knowing if the file handles are released properly.

Welcome to debugIO

  1. The file has hooks to add file open requests to a global list and file close requests are deleted from the global list.

All IOStream operations of the files need to be opened with util_open() and closed with util_close(). Hence, BufferedInputStream Base.close() has been overridden to ensure util_close() is called on the final IOStream.

  1. When you are creating temporary files the random nature of the temp files make readability and tractability harder for the temp files. A running counter is introduced to ensure debugging becomes easier in such cases.

  2. utilPrintOpenFiles() is introduced that will print and return the list of open files.

  3. util_open() and util_close() will print the file that's being opened or closed respectively.

To use ensure that you include the file in the testcase file runtests.jl

Note: Thanks to @tkelman for finding out the temporary files were not getting cleaned up.

Clone this wiki locally