Skip to content

Commit

Permalink
Another attempt to fix socket memory leak
Browse files Browse the repository at this point in the history
Ideally should fix both #3726 and the regression in #3753
  • Loading branch information
shai-almog committed Oct 28, 2023
1 parent 39f3132 commit c53f629
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions CodenameOne/src/com/codename1/io/Socket.java
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,13 @@ public synchronized void reset() throws IOException {
}

@Override
public void close() throws IOException {
closed = true;
if(Util.getImplementation().isSocketConnected(impl)) {
Util.getImplementation().disconnectSocket(impl);
con.setConnected(false);
public synchronized void close() throws IOException {
if(!closed) {
closed = true;
if (Util.getImplementation().isSocketConnected(impl)) {
Util.getImplementation().disconnectSocket(impl);
con.setConnected(false);
}
}
}

Expand Down Expand Up @@ -335,7 +337,7 @@ public int read() throws IOException {
protected void finalize() throws Throwable {
try {
close();
} catch (IOException err) {
} catch (Throwable err) {
Log.e(err);
}
}
Expand All @@ -350,8 +352,8 @@ static class SocketOutputStream extends OutputStream {
}

@Override
public void close() throws IOException {
if(Util.getImplementation().isSocketConnected(impl)) {
public synchronized void close() throws IOException {
if (con.isConnected() && Util.getImplementation().isSocketConnected(impl)) {
Util.getImplementation().disconnectSocket(impl);
con.setConnected(false);
}
Expand Down Expand Up @@ -397,7 +399,7 @@ public void write(int b) throws IOException {
protected void finalize() throws Throwable {
try {
close();
} catch (IOException err) {
} catch (Throwable err) {
Log.e(err);
}
}
Expand Down

0 comments on commit c53f629

Please sign in to comment.