Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Resource leak warning for long-lived Autoclosable object
Resource leak warning for long-lived Autoclosable object [message #1772481] Mon, 11 September 2017 09:57 Go to next message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
I have some code that gets a resource from somewhere and calls a method on it to modify it. The problem is that the resource is long-lived and cannot be closed after the method invocation. The code is similar to the following example where a warning is shown in function foo():
public class Test implements AutoCloseable {
	private Resource res = new Resource();
	
	private static class Resource implements AutoCloseable {
		private boolean closed = false;
		@Override
		public void close() throws Exception {
			assert !closed: "double close!";
			closed = true;
		}
		
		public void bar() {
			
		}
	}
	
	private Object getResource() {
		return res;
	}
	
	private void foo() {
		Object obj = getResource();
		
		if (obj instanceof Resource) {
			Resource r = (Resource)obj; // warning Resource leak: 'r' is never closed
			r.bar();			
		}
	}
	
	private void fooFixed() throws Exception {
		Object obj = getResource();
		
		if (obj instanceof Resource) {
			try(Resource r = (Resource)obj) {
				r.bar();			
			}
		}		
	}
	
	@Override
	public void close() throws Exception {
		res.close();
	}
	
	public static void main(String[] args) {
		try(Test test = new Test()) {
			test.fooFixed();
		}
		catch (Exception e) {
			
		}
	}
}


I think that eclipse assumes that getResource() always returns a new object which shall be closed. However, in this case, closing it would be wrong because the resource outlives the method invocation. The resource is finally closed because Test is AutoClosable, but when a user now just fixes the resource leak as in fooFixed(), the resource will be closed twice which is an error.
Re: Resource leak warning for long-lived Autoclosable object [message #1772592 is a reply to message #1772481] Tue, 12 September 2017 18:09 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1526
Registered: July 2009
Senior Member
We already have logic for detecting resources that are "shared" with code outside the current method, and obtaining a resource from a method call is one of the patterns we detect. I could imagine that this logic just cannot see beyond the cast. Can you try what happens if getResource() returns a Resource directly, without cast and assignment to a new local variable? It *should* downgrade the problem to a "potential resource leak", does it?
Re: Resource leak warning for long-lived Autoclosable object [message #1772614 is a reply to message #1772592] Wed, 13 September 2017 06:47 Go to previous messageGo to next message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
Hi Stephan,

I just tested it with a modified function returning Resource and not Object. As you predicted, the problem is downgraded to a potential resource leak.

But shouldn't it be easy to prove that there is no leak in this case? It should be easy to track the paths and prove that the resource is eventually closed on every path.
Re: Resource leak warning for long-lived Autoclosable object [message #1773088 is a reply to message #1772614] Thu, 21 September 2017 10:10 Go to previous message
Jens Auer is currently offline Jens AuerFriend
Messages: 11
Registered: February 2017
Junior Member
Hi,

I found another work-around. The false-positive vanishes if you move the cast into a simple helper method that casts from Object to Resource.
Previous Topic:Java Development related questions.
Next Topic:Eclipse - Debugging Issue about F5
Goto Forum:
  


Current Time: Mon Dec 11 13:11:17 GMT 2017

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

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