IntStream flagged as potential resource leak [message #1764407] |
Mon, 29 May 2017 13:39 |
Jens Auer Messages: 11 Registered: February 2017 |
Junior Member |
|
|
I have some code where I use java.util.IntStream similar to this one:
public void f(Stream<Object> s) {
IntStream n = s.mapToInt(Object::hashCode);
IntStream n2 = IntStream.range(23, 42);
n.forEach(i -> System.out.println(i));
n2.forEach(i -> System.out.println(i));
}
Eclipse gives me warning that n and n2 are potential resource leaks. I cannot see the reason for this, so I am wondering if this is a (known) bug?
[Updated on: Mon, 29 May 2017 14:19] Report message to a moderator
|
|
|
|
Re: IntStream flagged as potential resource leak [message #1764417 is a reply to message #1764414] |
Mon, 29 May 2017 14:48 |
Jens Auer Messages: 11 Registered: February 2017 |
Junior Member |
|
|
Hi David,
I think there is some inconsistency in the static analyzer in Eclipse in this case. As you said, BaseStream implements AutoClosable, and thus do both IntStream and Stream. However, only the specialisations for primitive types get flagged:
public void f3(Stream<Object> s) {
DoubleStream n = s.mapToDouble(x -> x.hashCode() * 1.0);
DoubleStream n2 = DoubleStream.generate(() -> 42.0);
n.forEach(i -> System.out.println(i));
n2.forEach(i -> System.out.println(i));
}
public void f(Stream<Object> s) {
IntStream n = s.mapToInt(Object::hashCode);
IntStream n2 = IntStream.range(23, 42);
n.forEach(i -> System.out.println(i));
n2.forEach(i -> System.out.println(i));
}
public void f2(Stream<Object> s) {
Stream<Integer> n = s.map(Object::hashCode);
Stream<Integer> n2 = Stream.generate(() -> 42);
n.forEach(i -> System.out.println(i));
n2.forEach(i -> System.out.println(i));
}
I get warnings in f and f3 for IntStream and DoubleStream, but not for f2 with Stream. Looks like there is a special case in the checker for Stream, but not for the specializations. The javadoc for BaseStream says thatQuote:
Streams have a BaseStream.close() method and implement AutoCloseable, but nearly all stream instances do not actually need to be closed after use. Generally, only streams whose source is an IO channel (such as those returned by Files.lines(Path, Charset)) will require closing. Most streams are backed by collections, arrays, or generating functions, which require no special resource management. (If a stream does require closing, it can be declared as a resource in a try-with-resources statement.)
So, for IntStream etc there is no need for AutoClosable, but it is just there in case some implementation needs it. I have a feeling that this class hierarchy is not optimal. It would probably be better to have factory functions which return a AutoClosableStream or a StreamWithoutAutoClosable depending on the arguments. But then this would have to transcend into the generator functions etc, so I am not sure if this is acutally feasible with Java.
[Updated on: Mon, 29 May 2017 14:57] Report message to a moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.04146 seconds