On Mon, 15 Dec 2025 14:05:51 GMT, Prasanta Sadhukhan <[email protected]> 
wrote:

>> Issue is when JTable is in editing mode, it is not Serializable as it gives 
>> exception
>> 
>> java.io.NotSerializableException: java.lang.reflect.Constructor
>>         at 
>> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1149) at 
>> java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1502)
>>         at 
>> java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1467)
>>         at 
>> java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1385)
>>         at 
>> java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1143) at 
>> java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1502)
>> .......
>> 
>> 
>> It is caused by creation of `GenericEditor` class which uses a 
>> non-serializable Constructor field.
>> This is fixed by making the field transient..
>> Also, `editorRemover` field is made transient as it is object of 
>> `CellEditorRemover` class which is not Serializable..
>
> Prasanta Sadhukhan has updated the pull request incrementally with one 
> additional commit since the last revision:
> 
>   Leakage fix

Leakage observation was red herring.
It seems the JTable instance we are inspecting after the read operation is 
potentially being observed in a test context where the Swing EDT hasn't fully 
processed all cleanup events yet so when the test is fixed to use EDT properly, 
it doesn't show the leak and shows componentCount as 2 which is `JTextField ` 
created in `createDefaultEditors` and `CellRendererPane` created in `installUI`


 public static void main(String[] args) throws Exception {
        StringBuilder str = new StringBuilder();
        testSerializeEditingTable(str);
        testSerializeNonEditingTable(str);
        if (str.length() != 0) {
            throw new RuntimeException(str.toString());
        }
    }

private static JTable jt;
    private static void testSerializeEditingTable(StringBuilder str) {
        try {
            Object[][] data = new Object[][]{ new Object[]{ 1,2,3,4,5}};
            Object[] names = new Object[]{ 1,2,3,4,5};
            while(true) {
                SwingUtilities.invokeAndWait(() -> {
                    jt = new JTable(data, names);
                    jt.editCellAt(0,3);
                });
                System.out.println("Serializing editing JTable");
                JTable newjt = serialize(jt);
                jt = newjt;
                // leak of the GenericEditor/JTextField, the number will grow 
over time and will cause OOM
                // and it is not possible to clean it after deserialisation we 
do not have a refs to it
                System.out.println("Count: " + jt.getComponentCount());
            }
        } catch (Exception e) {
            str.append("Failed serializing editing table " + e);
        }

-------------

PR Comment: https://git.openjdk.org/jdk/pull/28627#issuecomment-3655820373

Reply via email to