Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4146510

Resource leak when sending AWT print jobs to printer

XMLWordPrintable

    • beta
    • x86
    • windows_95



      Name: moC74494 Date: 06/08/98


      **********
      1. Steps
      **********

      (a) Compile and run the source code ('TESTPRINT') shown in (2) below.

      (b) Type a line of text in the dialog that appears and press the Print button.

      (c) Press OK in the Windows Print dialog, which should send the job to a printer.

      Each time steps (b) and (c) and carried out, two objects are allocated space in
      one of the three TESTPRINT modules (processes) that should be executing.
      These objects are not destroyed after the print job completes. As further print
      jobs are sent, these objects continue to accumulate.
      These are shown in (4) below.

      In addition, there is a "data" object, which contains the text to be printed, that
      is created in another of the three TESTPRINT modules. These, too, continue to accumulate
      as further print jobs are sent. This object is also shown in (4) below.

      We used HeapWatch (utiltity included in the Microsoft Platform SDK) to
      track the creation of these objects.

      **************
      2. Source Code
      **************

      // This example is from the book _Java AWT Reference_ by John Zukowski.
      // Written by John Zukowski. Copyright (c) 1997 O'Reilly & Associates.
      // You may study, use, modify, and distribute this example for any purpose.
      // This example is provided WITHOUT WARRANTY either expressed or


      import java.awt.*;
      import java.awt.event.*;
      import java.io.*;
      import java.util.Properties;

      public class TestPrint extends Frame {
        TextArea textArea;
        Label statusInfo;
        Button loadButton, printButton, closeButton;
        Properties p = new Properties();
          
        public TestPrint() {
          super ("File Loader");
          add (statusInfo = new Label(), "North");
          Panel p = new Panel ();
          p.add (loadButton = new Button ("Load"));
          loadButton.addActionListener( new LoadFileCommand() );
          p.add (printButton = new Button ("Print"));
          printButton.addActionListener( new PrintCommand() );
          p.add (closeButton = new Button ("Close"));
          closeButton.addActionListener( new CloseCommand() );
          add (p, "South");
          add (textArea = new TextArea (10, 40), "Center");
          pack();
        }
        public static void main (String args[]) {
          TestPrint f = new TestPrint();
          f.show();
        }

        // Bail Out
        class CloseCommand implements ActionListener {
          public void actionPerformed (ActionEvent e) {
            System.exit (0);
          }
        }
          
        // Load a file into the text area.
        class LoadFileCommand implements ActionListener {
          public void actionPerformed (ActionEvent e) {
            int state;
            String msg;
            FileDialog file = new FileDialog (TestPrint.this, "Load File", FileDialog.LOAD);
            file.setFile ("*.java"); // Set initial filename filter
            file.show(); // Blocks
            String curFile;
            if ((curFile = file.getFile()) != null) {
              String filename = file.getDirectory() + curFile;
              char[] data;
              setCursor (Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
              File f = new File (filename);
              try {
                FileReader fin = new FileReader (f);
                int filesize = (int)f.length();
                data = new char[filesize];
                fin.read (data, 0, filesize);
              } catch (FileNotFoundException exc) {
                String errorString = "File Not Found: " + filename;
                data = errorString.toCharArray ();
              } catch (IOException exc) {
                String errorString = "IOException: " + filename;
                data = errorString.toCharArray ();
              }
              statusInfo.setText ("Load: " + filename);
              textArea.setText (new String (data));
              setCursor (Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
            }
          }
        }

        // Print a file into the text area.
        class PrintCommand implements ActionListener {
          public void actionPerformed (ActionEvent e) {
            PrintJob pjob = getToolkit().getPrintJob(TestPrint.this, "Cool Stuff", p);
            if (pjob != null) {
              Graphics pg = pjob.getGraphics();
              if (pg != null) {
                String s = textArea.getText();
                printLongString (pjob, pg, s);
                pg.dispose();
              }
              pjob.end();
            }
          }
        }

        // Print string to graphics via printjob
        // Does not deal with word wrap or tabs
        void printLongString (PrintJob pjob, Graphics pg, String s) {
          int pageNum = 1;
          int linesForThisPage = 0;
          int linesForThisJob = 0;
          // Note: String is immutable so won't change while printing.
          if (!(pg instanceof PrintGraphics)) {
            throw new IllegalArgumentException ("Graphics context not PrintGraphics");
          }
          StringReader sr = new StringReader (s);
          LineNumberReader lnr = new LineNumberReader (sr);
          String nextLine;
          int pageHeight = pjob.getPageDimension().height;
          Font helv = new Font("Helvetica", Font.PLAIN, 12);
          //have to set the font to get any output
          pg.setFont (helv);
          FontMetrics fm = pg.getFontMetrics(helv);
          int fontHeight = fm.getHeight();
          int fontDescent = fm.getDescent();
          int curHeight = 0;
          try {
            do {
              nextLine = lnr.readLine();
              if (nextLine != null) {
                if ((curHeight + fontHeight) > pageHeight) {
                  // New Page
                  System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
                  pageNum++;
                  linesForThisPage = 0;
                  pg.dispose();
                  pg = pjob.getGraphics();
                  if (pg != null) {
                    pg.setFont (helv);
                  }
                  curHeight = 0;
                }
                curHeight += fontHeight;
                if (pg != null) {
                  pg.drawString (nextLine, 0, curHeight - fontDescent);
                  linesForThisPage++;
                  linesForThisJob++;
                } else {
                  System.out.println ("pg null");
                }
              }
            } while (nextLine != null);
          } catch (EOFException eof) {
            // Fine, ignore
          } catch (Throwable t) { // Anything else
            t.printStackTrace();
          }
          System.out.println ("" + linesForThisPage + " lines printed for page " + pageNum);
          System.out.println ("pages printed: " + pageNum);
          System.out.println ("total lines printed: " + linesForThisJob);
        }
      }
       

      ******************
      3. Error messages
      ******************

      None.


      *********************
      4. Trace Information
      *********************


      OBJECT ONE - Movable Size = 76 bytes.
      0000 08 00 11 00 27 00 01 00-57 49 4e 53 50 4f 4f 4c ....'...WINSPOOL
      0010 00 48 50 20 4c 61 73 65-72 4a 65 74 20 34 53 69 .HP LaserJet 4Si
      0020 20 53 6f 75 74 68 00 5c-5c 43 61 74 66 69 73 68 South.\\Catfish
      0030 5c 68 70 34 73 69 2d 70-71 31 00 00 00 00 00 00 \hp4si-pq1......
      0040 00 00 00 00 00 00 00 00-00 00 00 00 ............

      OBJECT TWO - Movable Size = 212 bytes.
      0000 48 50 20 4c 61 73 65 72-4a 65 74 20 34 53 69 20 HP LaserJet 4Si
      0010 53 6f 75 74 68 00 65 72-20 52 6f 6f 6d 00 00 00 South.er Room...
      0020 00 04 01 04 94 00 40 00-03 77 00 04 01 00 01 00 ......@..w......
      0030 00 00 00 00 00 00 01 00-01 00 fc ff 01 00 01 00 ................
      0040 58 02 02 00 00 00 00 00-00 00 00 00 00 00 00 00 X...............
      0050 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
      0060 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
      0070 00 00 00 00 00 00 00 00-00 00 00 00 00 00 00 00 ................
      0080 00 00 00 00 00 00 00 00-02 00 00 00 00 00 00 00 ................
      0090 00 00 00 00 01 00 40 00-4d 53 55 44 4e 03 48 50 ......@.MSUDN.HP
      00a0 20 4c 61 73 65 72 4a 65-74 20 34 53 69 00 00 00 LaserJet 4Si...
      00b0 00 00 00 00 00 00 00 00-00 00 00 00 00 00 e5 01 ................
      00c0 00 00 0d 01 00 00 3b 01-00 00 00 00 04 00 64 00 ......;.......d.
      00d0 0a 00 00 00 ....


      Allocated Object, 1456 Bytes (dependent on size of text string)

      0000 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0010 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0020 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0030 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0040 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0050 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0060 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0070 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0080 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0090 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      00a0 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      00b0 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      00c0 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      00d0 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      00e0 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      00f0 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0100 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0110 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0120 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0130 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0140 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0150 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0160 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0170 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0180 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0190 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      01a0 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      01b0 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      01c0 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      01d0 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      01e0 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      01f0 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0200 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0210 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0220 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0230 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0240 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0250 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0260 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0270 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0280 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0290 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      02a0 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      02b0 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      02c0 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      02d0 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      02e0 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      02f0 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0300 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0310 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0320 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0330 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0340 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0350 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0360 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0370 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0380 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0390 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      03a0 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      03b0 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      03c0 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      03d0 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      03e0 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      03f0 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0400 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0410 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0420 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0430 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0440 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0450 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 0a 00 S.t.r.i.n.g. ...
      0460 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0470 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0480 74 00 72 00 69 00 6e 00-67 00 20 00 54 00 65 00 t.r.i.n.g. .T.e.
      0490 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      04a0 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      04b0 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      04c0 53 00 74 00 72 00 69 00-6e 00 67 00 0a 00 54 00 S.t.r.i.n.g...T.
      04d0 65 00 73 00 74 00 53 00-74 00 72 00 69 00 6e 00 e.s.t.S.t.r.i.n.
      04e0 67 00 20 00 54 00 65 00-73 00 74 00 53 00 74 00 g. .T.e.s.t.S.t.
      04f0 72 00 69 00 6e 00 67 00-20 00 54 00 65 00 73 00 r.i.n.g. .T.e.s.
      0500 74 00 53 00 74 00 72 00-69 00 6e 00 67 00 20 00 t.S.t.r.i.n.g. .
      0510 54 00 65 00 73 00 74 00-53 00 74 00 72 00 69 00 T.e.s.t.S.t.r.i.
      0520 6e 00 67 00 20 00 54 00-65 00 73 00 74 00 53 00 n.g. .T.e.s.t.S.
      0530 74 00 72 00 69 00 6e 00-67 00 0a 00 54 00 65 00 t.r.i.n.g...T.e.
      0540 73 00 74 00 53 00 74 00-72 00 69 00 6e 00 67 00 s.t.S.t.r.i.n.g.
      0550 20 00 54 00 65 00 73 00-74 00 53 00 74 00 72 00 .T.e.s.t.S.t.r.
      0560 69 00 6e 00 67 00 20 00-54 00 65 00 73 00 74 00 i.n.g. .T.e.s.t.
      0570 53 00 74 00 72 00 69 00-6e 00 67 00 20 00 54 00 S.t.r.i.n.g. .T.
      0580 65 00 73 00 74 00 53 00-74 00 72 00 69 00 6e 00 e.s.t.S.t.r.i.n.
      0590 67 00 20 00 54 00 65 00-73 00 74 00 53 00 74 00 g. .T.e.s.t.S.t.
      05a0 72 00 69 00 6e 00 67 00-20 00 20 00 00 00 00 00 r.i.n.g. . .....



      **************************
      5. Additional Information
      **************************

      This bug appears to be occurring also on the 1.1.2 JDK supplied with Borland's JBuilder,
      but the above trace information is from a Sun 1.1.5 JDK execution.

      The source code was compiled on the Borland 1.1.2 JDK.
      (Review ID: 33184)
      ======================================================================

            dmendenhsunw David Mendenhall (Inactive)
            moanceasunw Mircea Oancea (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: