简体   繁体   English

这是资源泄漏还是误报?

[英]Is this a resource leak or a false positive?

Eclipse gives me a warning on the declaration of "out". Eclipse给出了关于“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 ". PS:警告是“ 资源泄漏:'out'永远不会关闭 ”。

It's a false positive. 这是误报。 All instances are correctly closed. 所有实例都已正确关闭。

I turned off those resource-related warnings in Eclipse long ago. 我很久以前在Eclipse中关闭了那些与资源相关的警告。 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. 它们确实不可靠,因为有很多“明显”正确的控制流路径,Eclipse无法将其识别为“正确”而没有实际执行它们......任何非平凡的代码都注定会产生这些误报。

It is definitely false positive , out is being assigned out1 or out2 which is being automatically closed. 这肯定是假阳性out被分配out1out2 ,它会被自动关闭。 Furthermore out is not visible outside try block. 此外,在try block外面看不到out。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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