• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner Variable Resource Leak  RSS feed

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

I've been making a number of scanner programs, just to get used to the idea and ive noticed the same thing in each program.

Every time I make the Scanner variable, Eclipse tells me that the variable is a resource leak (it underlines the Scanners name in yellow).


Why does this happen? and how can I fix it?
I've been looking for more information but aside from generalities, I cant seem to find anything specific (links or advice would be welcome)
(Eclipse does give 2 suggestions as to how to close the leak-but I'd rather know what im doing before I do it)


(the following is an example of one of my Scanner programs)

 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel,
I added code tags to your post to make it easier to read. You can do that yourself next time. Just highlight the code and click "code." Now, onto your question.

Eclipse is trying to protect you have leaking file handles. Scanner has a few constructors. Some take an InputStream or Readable. Which means someone can write code like new Scanner(new BufferedReader(...))

When opening a stream/reader, not closing it is usually a resource leak. Eclipse is picking up on this. In fact System.in is an InputStream. The problem here is that Eclipse is wrong. You don't close System.in. Making it ok to take Eclipse's advice to suppress the error. I recommend doing it on the most granular level of your choices. That way, you don't accidentally hide other real warnings in code you write later.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It shows the hazards of IDEs which are too helpful for their own good

Somebody else had problems about closing System.in: look here. You will also see I have my own opinion of how you use a Scanner pointing to System.in: in a utility class.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!