pdfbox-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tilman Hausherr <THaush...@t-online.de>
Subject Re: PDPageContentStream#close() vs PDDocument#close()
Date Thu, 22 Jun 2017 16:35:08 GMT
Am 21.06.2017 um 23:30 schrieb Thad Humphries:
> Is it necessary to call PDDocument#close() after calling
> PDPageContentStream#close()? Does the answer apply all cases or only
> certain cases? If the latter, what certain cases?

Yes it is needed to close your document too. If you don't, you'll risk 
memory leaks and other weird effects. The best is to use jdk7 
try-with-resources.

>
> For example, in the following code snippet:
>
> PDDocument  document = new PDDocument();
> PDPageContentStream cos = new PDPageContentStream(document, 0);
> cos.drawImage(... , etc.
> cos.close();
> document.close();
>
>
> Is the last line, `document.close()`, necessary, or has that been handled
> sufficiently by the `cos.close()` immediately before it?

Yes it is needed.

>
> Finally, I'm assuming that it is safe to call #close() a second (or third?)
> time on a PDDocument or PDPageContentStream. Is that correct? My use case
> would be in the finally block where an exception might have left PDDocument
> or PDPageContentStream open.
>

And yes you can close several times, the Closeable interface requires 
this. Although I'd suggest to work on your design (if still possible) if 
you're not sure where you're closing. IMHO it is always best to close 
where you open. And yes, even PDFBox is "guilty" at some places....

Tilman


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@pdfbox.apache.org
For additional commands, e-mail: users-help@pdfbox.apache.org


Mime
View raw message