Bug 361407 - Resource leak warning when resource is assigned to a field outside of constructor
Summary: Resource leak warning when resource is assigned to a field outside of constru...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 3.8 M5   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-10-19 10:24 EDT by Markus Keller CLA
Modified: 2012-01-24 08:50 EST (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Markus Keller CLA 2011-10-19 10:24:40 EDT
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();
    }
}
Comment 1 Deepak Azad CLA 2011-10-19 11:15:35 EDT
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'.
Comment 2 Stephan Herrmann CLA 2011-10-19 17:07:47 EDT
Makes sense. I'll take a look.
Comment 3 Stephan Herrmann CLA 2012-01-09 18:15:59 EST
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.
Comment 4 Stephan Herrmann CLA 2012-01-15 11:11:13 EST
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.
Comment 5 Ayushman Jain CLA 2012-01-24 08:50:45 EST
Verified for 3.8M5 using build I20120122-2000