Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » IntStream flagged as potential resource leak
IntStream flagged as potential resource leak [message #1764407] Mon, 29 May 2017 13:39 Go to next message
Jens Auer is currently offline Jens AuerFriend
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 #1764414 is a reply to message #1764407] Mon, 29 May 2017 14:41 Go to previous messageGo to next message
David Wegener is currently offline David WegenerFriend
Messages: 1445
Registered: July 2009
Senior Member
IntStreams extend the AutoCloseable through the BaseStream interface. The presence of this interface indicates that it is possible that an instance holds onto resources that need to be cleaned up. This generally isn't the case for IntStream, but since it includes AutoCloseable in its hierarchy, it will get flagged. Have a look at the AutoCloseable javadoc for a more complete explaination. https://docs.oracle.com/javase/8/docs/api/java/lang/AutoCloseable.html
Re: IntStream flagged as potential resource leak [message #1764417 is a reply to message #1764414] Mon, 29 May 2017 14:48 Go to previous messageGo to next message
Jens Auer is currently offline Jens AuerFriend
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

Re: IntStream flagged as potential resource leak [message #1769938 is a reply to message #1764417] Sun, 06 August 2017 17:23 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1853
Registered: July 2009
Senior Member
Sorry for late answer.

When j.u.s.Stream was introduced in Java 8, we added this to our internal white list of resource-less closeables, so that no warning will be issued.
IntStream is not automatically covered via subtyping, because we found examples of subclasses of resource-less closeables which themselves actually do hold a resource.

If s.o. files a bug against JDT/Core we can consider adding IntStream etc. to the white list.

PS: With project valhalla, the need for classes like IntStream will go away (but the class itself probably will stay).
Re: IntStream flagged as potential resource leak [message #1769974 is a reply to message #1769938] Mon, 07 August 2017 08:48 Go to previous message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
Hi,

thanks for the reply. I've opened a new bug (https://bugs.eclipse.org/bugs/show_bug.cgi?id=520609).

I also hope that reified generics will make it into the language, but I am also sceptical if this will ever happen due to backwards compatibility issues.
Previous Topic:How to implement a self defined LaunchDelegate to bebug java code?
Next Topic:Neon 2 uninstall. How ?
Goto Forum:
  


Current Time: Mon Dec 30 17:45:03 GMT 2024

Powered by FUDForum. Page generated in 0.04146 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top