Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 386534 - [compiler][resource] "Potential resource leak" false positive warning
Summary: [compiler][resource] "Potential resource leak" false positive warning
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.2   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: 4.3 M2   Edit
Assignee: Stephan Herrmann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-08-03 02:27 EDT by Ted Hopp CLA
Modified: 2012-09-18 02:05 EDT (History)
2 users (show)

See Also:


Attachments
Snippet (975 bytes, text/plain)
2012-08-03 07:07 EDT, Tomasz Zarna CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Ted Hopp CLA 2012-08-03 02:27:01 EDT
The following code generates a warning at the first line of the try block:

static void saveDetails(byte[] detailsData) {
    OutputStream os = null;
    try {
        os = sContext.openFileOutput(DETAILS_FILE_NAME, Context.MODE_PRIVATE);
        os.write(detailsData);
    } catch (IOException e) {
        Log.w(LOG_TAG, "Unable to save details", e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException ignored) {
            }
        }
    }
}

The method openFileOutput is declared to throw FileNotFoundException. Oddly, similar code in other files does not generate the warning.

The exact wording is: Potential resource leak: 'os' may not be closed
Comment 1 Tomasz Zarna CLA 2012-08-03 07:07:57 EDT
Created attachment 219521 [details]
Snippet

Having the attached snippet in workspace I see no warnings with default settings. Tod, could you check if I haven't twisted your example. Could you also try opening the same piece of code in a fresh workspace (to make sure you're running with default prefs).
Comment 2 Stephan Herrmann CLA 2012-08-03 10:42:44 EDT
(In reply to comment #1)
> Having the attached snippet in workspace I see no warnings with default
> settings. Tod, could you check if I haven't twisted your example. Could you
> also try opening the same piece of code in a fresh workspace (to make sure
> you're running with default prefs).

Potential resource leaks are ignored by default. That should explain this difference, right?

From looking at the code I'd say this is a duplicate of bug 385415. See there for background and workaround.

I will re-open if that guess is wrong.

*** This bug has been marked as a duplicate of bug 385415 ***
Comment 3 Ted Hopp CLA 2012-08-03 11:00:12 EDT
> Potential resource leaks are ignored by default. That should explain this
> difference, right?

Correct. I turned on most warnings, so this would not show up in a fresh workspace with default settings.

> From looking at the code I'd say this is a duplicate of bug 385415. See
> there for background and workaround.

At first I didn't think it was a duplicate, because bug 385415 does not involve a finally clause, either in the bug or the proposed workarounds. However, it turns out that the warning goes away if I eliminate the try/catch surrounding the call to os.close(), which is kind of what I get out of bug 385415 after reading it over a couple of times. If that's how 385415 is understood, then yes, this is a duplicate.

Would it be of value to add this case as a comment to that bug? It seems to be a slightly different code pattern than anything there. Or is having it here enough?
Comment 4 Stephan Herrmann CLA 2012-08-03 13:12:33 EDT
(In reply to comment #3)
> Would it be of value to add this case as a comment to that bug? It seems to
> be a slightly different code pattern than anything there. Or is having it
> here enough?

Adding this example as a test is a good idea, but having it in this bug is good enough. Thanks.
Comment 5 Stephan Herrmann CLA 2012-08-25 17:22:54 EDT
Bad news: work on bug 385415 shows that this is *not* a duplicate.

This bug can be described as a result of out-of-order analysis:
- the analysis first sees "os.close()" and assigns a resource tracker to os (using this tracker the resource is marked as closed)
- later it sees "os = sContext.openFileOutput(..)" handling this as re-assigning a new resource to the same variable, creating a new tracker which is marked open.
I.e., analysis "believes" we have two resources, of which only one is closed

This is due to the internal ordering inside TryStatement.analyseCode(..), which is an intricate issue (designed long before I joined the team).

As a result this bug must be investigated anew.
Comment 6 Stephan Herrmann CLA 2012-09-15 18:55:54 EDT
Test & fix have been released for 4.3 M2 via commit 0579ec46f093e76ebfb96f54aeedde906bdc6daa.

In bug 388996 I fixed the general problem of out-of-order analysis as described in comment 5.
For this bug I only had to improve the detection of that pattern: the analysis location can be (deeply) nested within a finally block -> iterate parents until null or FinallyFlowContext.
Comment 7 Srikanth Sankaran CLA 2012-09-18 02:05:51 EDT
Verified for 4.3 M2 using Build id: I20120917-0800