Skip to content

Instantly share code, notes, and snippets.

@spullara
Created July 3, 2012 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spullara/3041647 to your computer and use it in GitHub Desktop.
Save spullara/3041647 to your computer and use it in GitHub Desktop.
Fixes memory leak in FileDescriptor
diff -r aa0ad405f70b src/solaris/classes/java/io/FileDescriptor.java
--- a/src/solaris/classes/java/io/FileDescriptor.java Thu Jun 28 14:11:05 2012 -0700
+++ b/src/solaris/classes/java/io/FileDescriptor.java Tue Jul 03 11:26:32 2012 -0700
@@ -24,8 +24,11 @@
*/
package java.io;
+
+import java.io.Closeable;
+import java.lang.Boolean;
import java.util.ArrayList;
-import java.util.List;
+import java.util.WeakHashMap;
/**
* Instances of the file descriptor class serve as an opaque handle
@@ -46,7 +49,7 @@
private int fd;
private Closeable parent;
- private List<Closeable> otherParents;
+ private WeakHashMap<Closeable, Boolean> otherParents;
private boolean closed;
/**
@@ -174,11 +177,11 @@
// first caller gets to do this
parent = c;
} else if (otherParents == null) {
- otherParents = new ArrayList<>();
- otherParents.add(parent);
- otherParents.add(c);
+ otherParents = new WeakHashMap<Closeable, Boolean>();
+ otherParents.put(parent, true);
+ otherParents.put(c, true);
} else {
- otherParents.add(c);
+ otherParents.put(c, true);
}
}
@@ -195,7 +198,7 @@
IOException ioe = null;
try (Closeable c = releaser) {
if (otherParents != null) {
- for (Closeable referent : otherParents) {
+ for (Closeable referent : otherParents.keySet()) {
try {
referent.close();
} catch(IOException x) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment