DulcetAirman icon

Leaking Constructor

DulcetAirman | PRO | 04/19/13 01:12:41 PM UTC | 0 ⭐ | 728 👁️ | Never ⏰ | []
Java |

880 B

|

None

|

0 👍

/

0 👎

package com.example.foo;
 
public class SomeClass {
  public static volatile SomeClass leak = null;
  public final String string;
 
  public SomeClass(final boolean tru3) {
    leak = this;
    if (tru3)// always true!
      throw new RuntimeException();
    // This is the only place where something is assigned to "string":
    this.string = "something";
  }
 
  @Override
  public String toString() {
    return this.string;
  }
 
  public static void main(final String[] args) throws Exception {
    Object object = "nothing";
    try {
      object = new SomeClass(true);
    } catch (final Exception e) {
    }
    System.out.printf("object = %s (%s)\n", object, object.getClass().getSimpleName());
    try {
      System.out.printf("leak = %s (%s)\n", leak, leak.getClass().getSimpleName());
    } catch (final Exception e) {
      System.out.println("" + e);
    }
  }
}

Comments