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

Regression: ObjectOutputStream writes same object into stream multiple times.

XMLWordPrintable

    • 1.2beta4
    • sparc
    • solaris_2.6
    • Not verified

      To reproduce the bug, first apply this patch to
      src/share/classes/java/awt/Frame.java:

      ------- Frame.java -------
      *** /tmp/dejYR0_ Sun Jun 7 16:04:52 1998
      --- Frame.java Sun Jun 7 16:01:50 1998
      ***************
      *** 131,137 ****
             * instead of 'this' so that garbage collection can still take
             * place correctly.
             */
      ! private SoftReference softThis;
        
            private static final String base = "frame";
            private static int nameCounter = 0;
      --- 131,137 ----
             * instead of 'this' so that garbage collection can still take
             * place correctly.
             */
      ! transient private SoftReference softThis;
        
            private static final String base = "frame";
            private static int nameCounter = 0;
      ***************
      *** 494,499 ****
      --- 494,500 ----
            private void readObject(ObjectInputStream s)
              throws ClassNotFoundException, IOException
            {
      + new Exception().printStackTrace();
              s.defaultReadObject();
        
              if (menuBar != null)
      ***************
      *** 508,513 ****
      --- 509,515 ----
                  ownedWindows = null;
              }
        
      + softThis = new SoftReference(this);
              addToFrameList();
            }
        }

      The patch corrects a different bug, and modifies readObject() to print a
      stack trace every time a Frame is deserialized.

      Now, execute the following test program:

      import java.awt.*;
      import java.io.*;

      public class SerializationListTest {
              public static void main(String[] args) throws Exception {
                      Frame f = new Frame();
                      FileOutputStream fos = new FileOutputStream("tmp");
                      ObjectOutputStream oos = new ObjectOutputStream(fos);
                      oos.writeObject(f);
                      oos.flush();
                      FileInputStream fis = new FileInputStream("tmp");
                      ObjectInputStream ois = new ObjectInputStream(fis);
                      Frame f2 = (Frame)ois.readObject();

                      Frame[] frames = Frame.getFrames();
                      System.out.println(frames.length);
                      System.exit(0);
              }
      }

      The output appears as follows:
      java.lang.Exception
              at java.awt.Frame.readObject(Frame.java:497)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectInputStream.invokeObjectReader(ObjectInputStream.java:1673)
              at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1153)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:363)
              at java.io.ObjectInputStream.inputClassFields(ObjectInputStream.java:1819)
              at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:494)
              at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1154)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:363)
              at java.io.ObjectInputStream.inputClassFields(ObjectInputStream.java:1819)
              at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:494)
              at java.awt.Window.readObject(Window.java:687)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectInputStream.invokeObjectReader(ObjectInputStream.java:1673)
              at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1153)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:363)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:226)
              at SerializationListTest.main(SerializationListTest.java:13)
      java.lang.Exception
              at java.awt.Frame.readObject(Frame.java:497)
              at java.lang.reflect.Method.invoke(Native Method)
              at java.io.ObjectInputStream.invokeObjectReader(ObjectInputStream.java:1673)
              at java.io.ObjectInputStream.inputObject(ObjectInputStream.java:1153)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:363)
              at java.io.ObjectInputStream.readObject(ObjectInputStream.java:226)
              at SerializationListTest.main(SerializationListTest.java:13)
      3

      The final '3' indicates that the application sees three instantiated Frames.
      This is incorrect; there are really only two Frames in the app. The value '3'
      appears because readObject() is being called twice instead of once, even though
      the test program only deserializes one object.

      If the test program is run with 1.2beta3, the output is:

      2

      This is the correct value, so this bug should be considered a beta4 regression.

            jfialli Joe Fialli
            dmendenhsunw David Mendenhall (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: