Uploaded image for project: 'PDFBox'
  1. PDFBox
  2. PDFBOX-5586

NullPointerException when closing RandomAccessReadMemoryMappedFile more than once

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 3.0.0 PDFBox
    • 3.0.0 PDFBox
    • IO
    • None

    Description

      Matt Bamberger posted an error on users@pdfbox.a.o

      I'm using pdfbox 3.0.0-alpha3
      When I use a RandomAccessReadMemoryMappedFile to load a PDDocument, I get a NullPointerException when I close it after the PDDocument is closed:

      try (RandomAccessReadMemoryMappedFile reader = new RandomAccessReadMemoryMappedFile(file)) {
        try (PDDocument doc = Loader.loadPDF(reader, null, null, null, null)) {
          // ...
        }
      }
      

      I found that it's because this.mappedByteBuffer is already null the second time around, so I think you can just wrap in a not-null check:

      public void close() throws IOException {
        if (this.fileChannel != null) {
          this.fileChannel.close();
        }
      
        if (this.mappedByteBuffer != null) { // add this
          Optional.ofNullable(this.unmapper).ifPresent((u) -> {
            u.accept(this.mappedByteBuffer); // exception is thrown in here
          });
          this.mappedByteBuffer = null;
        }
      }
      

      Attachments

        Activity

          People

            lehmi Andreas Lehmkühler
            lehmi Andreas Lehmkühler
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: