Eclipse 4.2 resource leak through separate closure method

I am using Eclipse 4.2 with resource leak warnings enabled.

This code creates, in my opinion, a false warning about a resource leak.

public static void test(){ InputStream in = null; try { in = new FileInputStream("A"); } catch (IOException e) { return; }finally{ close(in); } } public static void close(Closeable closeable) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } 

If I refactor the code and pull the closure method into a finally block, everything will be fine.

  public static void test2(){ InputStream in = null; try { in = new FileInputStream("A"); } catch (IOException e) { return; }finally{ if (in != null) { try { in.close(); } catch (IOException e) { e.printStackTrace() } } } } 

Is it possible to somehow remove these warnings without duplicating the close method code and disabling resource leak warnings?

I found an error report here for something similar in loops, but there are no loops in my code.

+4
source share
2 answers
  • You should see a warning "Potential resource leak ...", if not, please open an error.
  • The main problem here is that the compiler does not know what the close (..) method does. It may or may not close the resource. (Note that the compiler does not perform interprocedural analysis)
  • You can ignore the Potential Resource Leakage warnings. (Resource leak warnings are guaranteed to be correct, but Potential ... are not.)

More information about resource leak analysis can be found here - http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-avoiding_resource_leaks.htm&cp=1_3_9_1

EDIT: the word “resource leak” and “potential resource leak”

  • The idea is that all reported resource leak warnings are guaranteed, but may not be “all” resource leaks in the code base.
  • "Potential resource leaks" are ... potential problems. Some developers include Potential ... warnings when they know something is going wrong, but they don’t know where. In such cases, potential alerts help narrow your search. Some other developers review potential warnings from time to time to see if there is a real problem.

Ideally, we would like the compiler to give us a complete and correct set of problems, but there are limitations in achieving this :-)

+3
source

Good News: Eclipse 4.3 Recognizes Google and Apache Utilities!

http://download.eclipse.org/eclipse/downloads/drops4/S-4.3M4-201212140730/news/

(Search "Leak Analysis")

+1
source

All Articles