简体   繁体   中英

Is this a resource leak or a false positive?

Eclipse gives me a warning on the declaration of "out". Is this a false positive?

Random r = new Random();
try(PrintWriter out1 = new PrintWriter("one.txt");
    PrintWriter out2 = new PrintWriter("two.txt"))
{
  PrintWriter out = r.nextBoolean()?out1:out2;
  out.println("x");
}

PS: The warning is " Resource leak: 'out' is never closed ".

It's a false positive. All instances are correctly closed.

I turned off those resource-related warnings in Eclipse long ago. They're really not reliable as there are so many "obviously" correct control flow paths that cannot be identified as "correct" by Eclipse without actually executing them... Any non-trivial code will be doomed to have those false positives.

It is definitely false positive , out is being assigned out1 or out2 which is being automatically closed. Furthermore out is not visible outside try block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM