Community
Participate
Working Groups
I20111018-0850 It's inconsistent that I get resource leak warnings when a resource is assigned to a field outside of constructor. Inside a constructor, the warning seems to be hidden. This happens whether or not dispose() is there. package xy; import java.io.FileInputStream; import java.io.IOException; import java.util.Objects; public class Leaks { private FileInputStream fInput; Leaks(String name) throws IOException { FileInputStream fileInputStream= new FileInputStream(name); fInput= fileInputStream; Objects.hashCode(fInput); init(name); } void init(String name) throws IOException { FileInputStream fileInputStream= new FileInputStream(name); fInput= fileInputStream; Objects.hashCode(fInput); } public void dispose() throws IOException { fInput.close(); } }
Also assignment of a resource to a field should be treated the same way as resource as a parameter or a return value i.e. the warning should be 'Potential resource leak' instead of 'Resource leak'.
Makes sense. I'll take a look.
The matter of resources stored in fields is covered by the patch in bug 358903. Actually just like a resource returned from a method also assignment to a field means we don't issue any warning. In both cases it's likely s.o. else's responsibility to close. However, the difference between method and ctor still needs to be looked at.
The issues of potential / definite leaks and field assignments has indeed been resolved in bug 358903. The remaining inconsistency between methods and constructors (incl. self-calls) has been fix in commit 96ef6a1342ca74d72728b34641d9caf80d6d6997.
Verified for 3.8M5 using build I20120122-2000