Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't Eclipse show leak warning for streams?

In Eclipse Neon, if I write this Java code:

Stream<Object> stream = Stream.builder().build();

I get no leak warnings, but if I implement Stream, such as

public class MyStream<T> implements Stream<T> {
    // implementation
}

and I write similar code

Stream<Object> stream = new MyStream<>();

I get a Resource leak: 'stream' is never closed warning. This happens only in Eclipse, while compiling with javac does not issue any warning.

Note I'm not looking for an answer on how to close the stream and such, but for an answer which explains the reason of this different behavior for the same interface.

like image 557
Giovanni Lovato Avatar asked Mar 21 '17 18:03

Giovanni Lovato


2 Answers

Eclipse has a whitelist of types that do not require cleanup, because they don't actually refer to system resources. Core Java types are listed here, but your custom types are not. See the help for more information.

like image 115
erickson Avatar answered Oct 22 '22 03:10

erickson


In the first case you are not creating the instance of the resource. In the second case, you are.

The eclipse documentation states the following:

Ownership / responsibility
The above diagnostics basically assume that a method that creates an instance of a resource type is also responsible for closing this resource. [...]
- If a method obtains a resource via a method call rather than by a new expression, it may or may not be responsible; any problems are only flagged as potential resource leaks.

like image 34
NickL Avatar answered Oct 22 '22 03:10

NickL