Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Juno - Spurious warning: Resource Leak
Juno - Spurious warning: Resource Leak [message #902109] Wed, 15 August 2012 21:42 Go to next message
Jim Garrison is currently offline Jim GarrisonFriend
Messages: 5
Registered: August 2012
Junior Member
Resource leak: 'br' is not closed at this location [indicated below]

SSCCE:
import java.io.BufferedReader;
import java.io.FileReader;

public class LeakTest
{
    public int a;
    
    public void test() throws Exception
    {
        BufferedReader br = null;
        try
        {
            br = new BufferedReader(new FileReader("a.txt"));
            String line;
            while ((line=br.readLine()) != null)
            {
                System.out.println(line);
                if (a == 1)
                    throw new RuntimeException("X"); // <<<<<<<<<<<<==== HERE
            }
        }
        finally
        {
            if (br != null) try { br.close(); } catch(Exception e) { }
        }
    }
}


The warning is issued ONLY if the throw is part of an if/then (either branch) inside a loop. Remove the "if" or loop and the warning is not flagged.
Re: Juno - Spurious warning: Resource Leak [message #902112 is a reply to message #902109] Wed, 15 August 2012 22:17 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
We have a group of related bug reports already, for closest resemblance see https://bugs.eclipse.org/361073#c7

Common theme: the JLS specifies flow analysis for definite assignment across try statements in a very conservative manner. JDT re-uses the same flow analysis for analysis of null pointers access and resource leaks yielding results that are less precise than we would like.

I have hopes that after the fix in https://bugs.eclipse.org/345305 has been released most of these "bugs" can be resolve quite easily.

Until then please refer to https://bugs.eclipse.org/385415#c10 f. for some advice how the code can be made easier to analyse for the compiler, notably by using the Java 7 construct try-with-resources!

cheers,
Stephan
Previous Topic:How to compile AST to bytecode
Next Topic:How can I use eclipse default package explorer to create my own package explorer
Goto Forum:
  


Current Time: Fri Apr 19 01:41:11 GMT 2024

Powered by FUDForum. Page generated in 0.01816 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top