Permalink
Browse files

Closed Listener at program exit

  • Loading branch information...
1 parent 268f950 commit 03eeeb32a3578b79be2c1e229e76821c54c93b05 @nathanh89 committed Jan 18, 2013
View
1 src/com/visionarysoftwaresolutions/budgetboss/cli/InputValidator.java
@@ -161,6 +161,7 @@ public static boolean itsTimeToExit(String toCheck){
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
BudgetBoss.doneUsingBudgetBoss();
+ Listener.create().closeListener();
return true;
}else
return false;
View
39 src/com/visionarysoftwaresolutions/budgetboss/cli/Listener.java
@@ -2,36 +2,37 @@
import java.io.BufferedReader;
import java.io.IOException;
-import java.io.InputStream;
import java.io.InputStreamReader;
-import java.io.Reader;
public class Listener{
- private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private String userInput = null;
+ private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private Listener(){}
-
- public static Listener create(){
- return new Listener();
- }
-
+
public void setReader(BufferedReader reader){
this.reader = reader;
}
+ public static Listener create(){
+ return new Listener();
+ }
+
public String getInput(){
- try {
- userInput = reader.readLine();
- } catch (IOException e) {
- System.out.println("I/O ERROR!?");
- }
- try {
- reader.close();
- } catch (IOException e) {
- System.out.println("I/O ERROR!");
- }
- return userInput;
+ try {
+ userInput = reader.readLine();
+ }catch (IOException e){
+ System.out.println("I/O ERROR!");
+ }
+ return userInput;
+ }
+
+ public void closeListener(){
+ try {
+ reader.close();
+ } catch (IOException e) {
+ System.out.println("I/O ERROR!");
+ }
}
}
View
1 src/com/visionarysoftwaresolutions/budgetboss/menu/EditorMenu.java
@@ -87,6 +87,7 @@ private void getNewName(){
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
BudgetBoss.doneUsingBudgetBoss();
+ Listener.create().closeListener();
}
}
View
1 src/com/visionarysoftwaresolutions/budgetboss/menu/MainMenu.java
@@ -122,6 +122,7 @@ private void exitProgram(){
BudgetBoss.doneUsingBudgetBoss();
BudgetBoss.endLoadSavedBudget();
BudgetBoss.endNeedNewBudget();
+ Listener.create().closeListener();
@leadVisionary
leadVisionary added a line comment Jan 20, 2013

This is incorrect. You're creating a new instance and then closing the reader that belongs to that one, not the one you used to populate the String userInput. If you explore some profiling tools, you should be able to diagnose a memory leak and see that you've nicely written a virus. :)

What you need to do is retain an instance of the original listener you create to get the user's input string (as opposed to getting a new one every time) and then call closeListener on that original instance.

Remember that object creation is expensive and needs to be carefully managed.
http://oreilly.com/catalog/javapt/chapter/ch04.html
http://en.wikipedia.org/wiki/Flyweight_pattern

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
}
public void chooseOption(int optionChose){
View
7 tests/com/visionarysoftwaresolutions/budgetboss/cli/tests/ListenerTests.java
@@ -28,4 +28,11 @@ public void testGetInput() {
//Then: TakeInput will get the correct String from the user
Assert.assertTrue(isNeeded.equals("Input from console!"));
}
+
+ @Test
+ public void testMulitpleInstantiationsRealInput(){
+ //This ensures that the reader doesn't close before the end of the program
+ String firstInput = Listener.create().getInput();
@leadVisionary
leadVisionary added a line comment Jan 20, 2013

You should hang on to the reference of the Listener you create and keep calling getInput() on that one, not create a new one every time.

Listener inputGrabber = Listener.create();
String firstInput = inputGrabber.getInput();
String secondInput = inputGrabber.getInput();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ String secondInput = Listener.create().getInput();
+ }
}

0 comments on commit 03eeeb3

Please sign in to comment.