Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 421035 - [resource] False alarm of resource leak warning when casting a closeable in its assignment
Summary: [resource] False alarm of resource leak warning when casting a closeable in i...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.2.2   Edit
Hardware: PC Windows 7
: P4 normal (vote)
Target Milestone: 4.5 M7   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-04 21:22 EST by Huang Ji Yong CLA
Modified: 2015-04-24 01:37 EDT (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Huang Ji Yong CLA 2013-11-04 21:22:16 EST

    
Comment 1 Huang Ji Yong CLA 2013-11-04 21:25:33 EST
Test case:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

public class Test {
  public static void main(String[] args) throws FileNotFoundException {
    Reader a = (Reader)new BufferedReader(new FileReader("a"));
    try {
		a.close();
	} catch (IOException e) {
		e.printStackTrace();
	}
  }
}

I get a warning at new BufferedReader(a) that says, "Resource leak: '<unassigned Closeable value>' is never closed".
I think it is a false warning. Please check.
Comment 2 Stephan Herrmann CLA 2013-11-06 17:06:36 EST
I can reproduce and there's an easy workaround, too:

Omit the unnecessary cast to Reader (why would you ever cast an
allocation expression? It already has the most precise type possible,
and up-casts are never needed anyway).

For some reason the cast obscures the fact that the BufferedReader
is assigned to 'a'.

I'll take a look, time permitting.
Comment 3 Stephan Herrmann CLA 2013-11-06 17:08:02 EST
Also note the warning issued at compliance 1.7:

1. WARNING in /tmp/Test.java (at line 9)
        Reader a = (Reader)new BufferedReader(new FileReader("a"));
               ^
Resource 'a' should be managed by try-with-resource


:)
Comment 4 Huang Ji Yong CLA 2013-11-06 19:38:57 EST
Stephan,
Thanks for looking at this problem. I know there is workaround, but our java code is generated and we need to add the additional cast. The case is just a simplified sample to demonstrate the problem.
Comment 5 Stephan Herrmann CLA 2014-04-12 09:21:26 EDT
This one *might* be simple enough to sneak in late into 4.4
Comment 6 Stephan Herrmann CLA 2014-04-15 06:59:22 EDT
Putting this issue on standby.
Comment 7 Eclipse Genie CLA 2015-04-02 12:20:32 EDT
New Gerrit change created: https://git.eclipse.org/r/45163
Comment 8 Stephan Herrmann CLA 2015-04-02 12:22:18 EDT
The immediate problem was easy to fix indeed: just peel-off the cast when inspecting the right hand side in the assignment.

Unfortunately this revealed some hidden bugs in the implementation (regressions in ResourceLeakTests). Here's what the patch under test will change in addition to the immediate fix:


More initialization of the bits in UFI that were introduced for bug 453483: If a flow info is growing (more than 64 locals), the newly allocated extra storage was lacking initialization for these new bits.
Fixed by:
- Inventing a new tag bit UNROOTED, to mark those flowInfos that need to set the IN/INN bits to -1L.
- Extracting and enhancing createExtraSpace(length)


FVT.reportRecordedErrors() under some circumstances reported problems even if the nullStatus was UNKNOWN. 
Fixed by:
- Consulting the null status, AND,
- Checking if perhaps all recorded locations agree on status NULL, for the case the flowInfo was unreachable (and therefore had no null info).


FTV.mergeCloseStatus() was merging NULL and UNKNOWN into POTENTIALLY_NULL, which is wrong when UNKNOWN really expresses: no null information. 
Fixed by one additional check.
Comment 10 Stephan Herrmann CLA 2015-04-02 13:40:16 EDT
.
Comment 11 Manoj N Palat CLA 2015-04-24 01:37:52 EDT
Verified for Eclipse 4.5 Mars M7 Build id: I20150422-1000