From 3d2702bab23041de9465badf9775337ea751c806 Mon Sep 17 00:00:00 2001 From: Simon Ebner Date: Tue, 16 Apr 2013 08:39:23 +0200 Subject: [PATCH] fixed potential memory leaks --- ch.psi.fdaq/pom.xml | 4 +-- .../main/java/ch/psi/fdaq/FdaqService.java | 28 +++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/ch.psi.fdaq/pom.xml b/ch.psi.fdaq/pom.xml index 6ca92d8..d0b58c1 100644 --- a/ch.psi.fdaq/pom.xml +++ b/ch.psi.fdaq/pom.xml @@ -9,12 +9,12 @@ i.snapshots Artifactory Snapshots - http://slsyoke1/artifactory/libs-snapshots-local + http://yoke.psi.ch/artifactory/libs-snapshots-local i.releases Atrifactory Releases - http://slsyoke1/artifactory/libs-releases-local + http://yoke.psi.ch/artifactory/libs-releases-local \ No newline at end of file diff --git a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java index a2dc72e..e33ad57 100644 --- a/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java +++ b/ch.psi.fdaq/src/main/java/ch/psi/fdaq/FdaqService.java @@ -50,12 +50,18 @@ public class FdaqService { @Override public void run() { + + PrintWriter writer = null; + Socket echoSocket = null; + DataOutputStream out = null; + DataInputStream in = null; + try { FdaqConfiguration config = FdaqConfiguration.getInstance(); stopAcquisition = false; - Socket echoSocket = new Socket(config.getHostname(), config.getPort()); - DataOutputStream out = new DataOutputStream(echoSocket.getOutputStream()); - DataInputStream in = new DataInputStream(echoSocket.getInputStream()); + echoSocket = new Socket(config.getHostname(), config.getPort()); + out = new DataOutputStream(echoSocket.getOutputStream()); + in = new DataInputStream(echoSocket.getInputStream()); int nMessages = config.getNelements(); @@ -70,7 +76,7 @@ public class FdaqService { // open file and write - try(PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(file)))){ + writer = new PrintWriter(new BufferedWriter(new FileWriter(file))); writer.println("# a" + "\t" + "b1" + "\t" + "b2" + "\t" + "c1" + "\t" + "c2" + "\t" + "d"); @@ -97,8 +103,8 @@ public class FdaqService { // System.out.println(a + " " + b1 + " " + b2 + " " + c1 + " " + c2 + " " + d); writer.println(a + "\t" + b1 + "\t" + b2 + "\t" + c1 + "\t" + c2 + "\t" + d); } - } - + + writer.close(); out.close(); in.close(); echoSocket.close(); @@ -107,6 +113,16 @@ public class FdaqService { logger.log(Level.SEVERE, "", e); } } + finally{ + try{ + writer.close(); + out.close(); + in.close(); + echoSocket.close(); + } catch (IOException e) { + // Ignore because not relevant at this stage + } + } } }); t.start();