7085981: XXSocket types depend on impl finalizer to close if constructor throws exception

Reviewed-by: alanb, chegar
This commit is contained in:
Michael McMahon 2011-09-09 14:04:44 +01:00
parent 6b8980e983
commit 518e846e10
3 changed files with 17 additions and 5 deletions

View File

@ -174,9 +174,7 @@ class DatagramSocket implements java.io.Closeable {
* @see SecurityManager#checkListen * @see SecurityManager#checkListen
*/ */
public DatagramSocket() throws SocketException { public DatagramSocket() throws SocketException {
// create a datagram socket. this(new InetSocketAddress(0));
createImpl();
bind(new InetSocketAddress(0));
} }
/** /**
@ -221,7 +219,12 @@ class DatagramSocket implements java.io.Closeable {
// create a datagram socket. // create a datagram socket.
createImpl(); createImpl();
if (bindaddr != null) { if (bindaddr != null) {
bind(bindaddr); try {
bind(bindaddr);
} finally {
if (!isBound())
close();
}
} }
} }

View File

@ -162,7 +162,12 @@ class MulticastSocket extends DatagramSocket {
setReuseAddress(true); setReuseAddress(true);
if (bindaddr != null) { if (bindaddr != null) {
bind(bindaddr); try {
bind(bindaddr);
} finally {
if (!isBound())
close();
}
} }
} }

View File

@ -425,6 +425,10 @@ class Socket implements java.io.Closeable {
} catch (IOException e) { } catch (IOException e) {
close(); close();
throw e; throw e;
} finally {
// if bind() or connect threw a runtime exception
if ((localAddr != null && !bound) || (address != null && !connected))
close();
} }
} }