New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Find resource leaks (AutoCloseables that should have been closed) #264
Comments
Maybe we could create an annotation to distinguish between the two cases? |
JSR 305 has some annotations which could be used: |
For any method whose return type implements AutoCloseable, or constructor of a concrete class that implements AutoCloseable, I suspect that it's most commonly the case that the caller does take on the responsibility to close that instance (or to return it to someone else, passing the buck). It would probably be more efficient for us to annotate the exceptions (and blacklist for the sources we can't annotate, like StringWriter constructor). |
I'm... sorta surprised I said that. It seems a lot more practical to try building up a whitelist of things that we know imply caller responsibility. I'm newly concerned about this issue because JDK 8 brings an attractive nuisance of a method called Files.lines(). People will use it like this:
This is bad. We need them to write
|
FYI, I glanced at 20 usages of Files.lines() and 9 of them were not closing. |
Ameliorates #264 RELNOTES: New check for Files.lines calls where the stream isn't closed ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=140624985
There's now a check specifically for |
Are there plans to support annotations like |
Original issue created by cushon@google.com on 2014-10-30 at 04:01 PM
Originally reported by kevinb9n:
Many ways of obtaining an AutoCloseable instance imply responsibility on the obtainer to close that instance (e.g. by obtaining it inside try-with-resources), and many don't. For error-prone to be able to detect resource leaks it would need some way to differentiate. How?
eaftan notes:
We can use dataflow to determine how the instance was obtained. Is it always the case that knowing the provenance of the instance is enough to know whether it needs to be closed? Can you provide examples other than try-with-resources?
The text was updated successfully, but these errors were encountered: