[ 
https://issues.apache.org/jira/browse/PDFBOX-4242?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16506095#comment-16506095
 ] 

Glen Peterson commented on PDFBOX-4242:
---------------------------------------

Thanks to the clarity from this discussion I was able to quickly eliminate my 
file descriptor leakage problem this morning.  To see how I worked around this 
issue, search for 'openFontFiles' on this page:
https://github.com/PlanBase/PdfLayoutMgr2/blob/master/src/main/java/com/planbase/pdf/layoutmanager/PdfLayoutMgr.kt

I don't know if I can work on this, but if I submit a patch, would it be OK to 
just load each font into memory and close the file descriptor right away?  I 
don't know why it ever kept the file descriptor open, or if the reasons for 
that are still valid.  If you still need the file descriptors open, we could 
keep track of them inside PdType0Font.load() and free them inside 
PDDocument.close() so that clients don't have to, similar to what I did above, 
but in Java 6.

I checked the usage example and it does the same thing I did, so it has a file 
descriptor leak.
https://github.com/apache/pdfbox/blob/trunk/examples/src/main/java/org/apache/pdfbox/examples/pdmodel/EmbeddedFonts.java#L49

> Fontbox does not close file descriptor when loading fonts.
> ----------------------------------------------------------
>
>                 Key: PDFBOX-4242
>                 URL: https://issues.apache.org/jira/browse/PDFBOX-4242
>             Project: PDFBox
>          Issue Type: Bug
>          Components: FontBox
>    Affects Versions: 2.0.9
>            Reporter: Glen Peterson
>            Priority: Minor
>              Labels: file_leak
>
> My app has been getting "java.io.FileNotFoundException (No file descriptors 
> available)" and I've confirmed that it's because fontbox isn't closing it's 
> file descriptors.
> In org.apache.fontbox.ttf.TTFParser there's this method:
> {{public TrueTypeFont parse(File ttfFile) throws IOException {}}
>  {{  RAFDataStream raf = new RAFDataStream(ttfFile, "r");}}
> {{  try {}}
>  {{    return this.parse((TTFDataStream)raf);}}
>  {{  } catch (IOException var4) {}}
>  {{    // close only on error (file is still being accessed later)}}
>  {{    raf.close();}}
>  {{    throw var4;}}
>  {{}}}
>  {{}}}
> I would have expected to see the close() in a finally block so that the file 
> is always closed, not just on exceptions. Presumably, you can keep it in 
> memory without leaving the file descriptor open?
> {{public TrueTypeFont parse(File ttfFile) throws IOException {}}
>  {{  RAFDataStream raf = new RAFDataStream(ttfFile, "r");}}
> {{  try {}}
>  {{    return this.parse((TTFDataStream)raf);}}
>  {{  } catch (IOException var4) {}}{{    raf.close();}}
>  {{    throw var4;}}
>  {{  } finally {}}
>  {{    raf.close();}}
>  {{}}}
>  {{}}}
> I tried performing this in a lazy initialization, but it blew up:
> java.lang.RuntimeException: java.io.IOException: The TrueType font null does 
> not contain a 'cmap' tableCaused by: java.io.IOException: The TrueType font 
> null does not contain a 'cmap' table
>   at 
> org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapImpl(TrueTypeFont.java:548)
>   at 
> org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapLookup(TrueTypeFont.java:528)
>   at 
> org.apache.fontbox.ttf.TrueTypeFont.getUnicodeCmapLookup(TrueTypeFont.java:514)
>   at org.apache.fontbox.ttf.TTFSubsetter.<init>(TTFSubsetter.java:91)
>   at 
> org.apache.pdfbox.pdmodel.font.TrueTypeEmbedder.subset(TrueTypeEmbedder.java:321)
>   at org.apache.pdfbox.pdmodel.font.PDType0Font.subset(PDType0Font.java:239)
>   at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1271)
> Thoughts?
> Thanks for PDFBox - it's been really helpful!



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to