Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » New Resource Leak Warnings...
New Resource Leak Warnings... [message #1823096] Thu, 19 March 2020 13:26 Go to next message
Ed Merks is currently offline Ed MerksFriend
Messages: 31543
Registered: July 2009
Senior Member
I'm noticing a bunch of new leak warnings, e.g., in this
  protected void replace(IFile file, String lineSeparator) throws IOException, CoreException
  {
    InputStream contents = file.getContents();
    String charset = file.getCharset();
    BufferedReader reader = new BufferedReader(new InputStreamReader(contents, charset));
    CharArrayWriter writer = new CharArrayWriter();
    int c;
    while ((c = reader.read()) != -1)
    {
      writer.write(c);
    }
    contents.close();
    String string = writer.toString();
    String newContents = LINE_DELIMITER_PATTERN.matcher(string).replaceAll(lineSeparator);
    byte[] bytes = newContents.getBytes(charset);
    file.setContents(new ByteArrayInputStream(bytes), IResource.FORCE, null);
  }
It complains that "reader" is never closed. This code has been like this for a long time.

Is behavior this a regression?


Ed Merks
Professional Support: https://www.macromodeling.com/
Re: New Resource Leak Warnings... [message #1823103 is a reply to message #1823096] Thu, 19 March 2020 14:15 Go to previous messageGo to next message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1828
Registered: July 2009
Senior Member
I can reproduce the warning since 4.15M1. Prior to that we only reported "should be managed with try-with-resources".

The warning is not issued when closing the outermost resource reader, instead of the innermost contents. I'll come back once I understand what exactly caused the change.
Re: New Resource Leak Warnings... [message #1823107 is a reply to message #1823103] Thu, 19 March 2020 14:35 Go to previous message
Stephan Herrmann is currently offline Stephan HerrmannFriend
Messages: 1828
Registered: July 2009
Senior Member
Over to https://bugs.eclipse.org/bugs/show_bug.cgi?id=561259

Thanks, Ed.
Previous Topic:Debuging
Next Topic:Redundant Null Check and Dead Code
Goto Forum:
  


Current Time: Thu Oct 22 21:28:30 GMT 2020

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

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

Back to the top