Uploaded image for project: 'Maven Doxia Tools'
  1. Maven Doxia Tools
  2. DOXIATOOLS-45

An incomplete fix for the resource leak bugs in LatexBookRenderer.java

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Critical
    • Resolution: Auto Closed
    • None
    • None
    • Doxia Book Renderer
    • None

    Description

      The fix revision 740164 was aimed to remove resource leak bugs on the FileWriter object "writer"(created in line 204) and the FileReader object "reader" (created in line 219) in the method "writeSection()"of the file " /maven/doxia/doxia/trunk/doxia-book/src/main/java/org/apache/maven/doxia/book/services/renderer/LatexBookRenderer.java" , but it is incomplete.

      There are some problems:
      1. the LatexBookSink object "sink" is not closed.
      2. when the statements at lines 206-215 throw some exception, "writer" will be leaked.

      The best way to close such resource objects is putting such close operations for all resource objects in the finaly block of a try-catch-finally structure.

      Although a later fix (rev1003021) try to close "sink", the problems still exists in the head revision. The buggy code is copied as bellows ("sink" and "writer" will be leaked when the statements at lines 202-207 throw some exception):

         private SectionInfo writeSection( Section section, BookContext context )
              throws IOException, BookDoxiaException
          {
              File file = new File( context.getOutputDirectory(), ( section.getId() + ".tex" ) );
      
      198        Writer writer = WriterFactory.newWriter( file, context.getOutputEncoding() );
      
      200        LatexBookSink sink = new LatexBookSink( writer );
      
              BookContext.BookFile bookFile = (BookContext.BookFile) context.getFiles().get( section.getId() );
      
              if ( bookFile == null )
              {
                  throw new BookDoxiaException( "No document that matches section with id="
                              + section.getId() + "." );
              }
      
              Reader reader = null;
              try
              {
                  reader = ReaderFactory.newReader( bookFile.getFile(), context.getInputEncoding() );
                  doxia.parse( reader, bookFile.getParserId(), sink );
              }
              catch ( ParserNotFoundException e )
              {
                  throw new BookDoxiaException( "Parser not found: "
                              + bookFile.getParserId() + ".", e );
              }
              catch ( ParseException e )
              {
                  throw new BookDoxiaException( "Error while parsing document: "
                              + bookFile.getFile().getAbsolutePath() + ".", e );
              }
              catch ( FileNotFoundException e )
              {
                  throw new BookDoxiaException( "Could not find document: "
                              + bookFile.getFile().getAbsolutePath() + ".", e );
              }
              finally
              {
      233            sink.flush();
      234            sink.close();
      
      236            IOUtil.close( reader );
      237            IOUtil.close( writer );
              }
      
              SectionInfo info = new SectionInfo();
              info.id = section.getId();
              info.title = sink.getTitle();
      
              return info;
          }
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            guangtai Guangtai Liang
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: