5
try(InputStream in = url.openStream(); Scanner scanner = new Scanner(in).useDelimiter("\\A")) {

} catch(IOException e) {
    e.printStackTrace();
}

The line

new Scanner(in)

gives the warning:

Resource leak: <unassigned Closeable> value' is never closed

It goes away if I remove useDelimiter(String).

useDelimiter(String) does not return a new instance (it returns this), so why am I getting this warning? Is it a bug?

I am using Eclipse 4.4. My question is not relevant to this, where a warning occurs due to a different situation

Community
  • 1
  • 1
Vince
  • 14,470
  • 7
  • 39
  • 84
  • It may be problem with eclipse. Check this link : http://help.eclipse.org/mars/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-avoiding_resource_leaks.htm. Same type of question is unanswered in SE: http://stackoverflow.com/questions/23459568/eclipse-inconsistencies-resource-leak-unassigned-closeable-value-is-never – Ravindra babu Mar 20 '16 at 06:02

2 Answers2

0

Yep, this looks like a bug in the leak checker. I would recommend filing a bug report. Initial googling seems to indicate that one doesn't already exist.

Although I guess you could claim that it is not possible to solve this problem properly. The issue is that it would be difficult to statically prove that useDelimiter returns this without a lot of special casing.

IRCBot
  • 51
  • 4
0

IntelliJ has this flaw too. For me, it doesn't seem hard at all for the resource checker to figure out that useDelimiter() returns this. So I think this could be a quick win if fixed in IntelliJ and/or Eclipse :)

Programmer Trond
  • 303
  • 2
  • 15