X-Received: by 2002:a0c:99e1:: with SMTP id y33mr637469qve.32.1551384689314; Thu, 28 Feb 2019 12:11:29 -0800 (PST) X-Received: by 2002:a5b:10c:: with SMTP id 12mr1167108ybx.323.1551384689127; Thu, 28 Feb 2019 12:11:29 -0800 (PST) Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder-in1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!m21no5270991qta.0!news-out.google.com!y15ni20qta.0!nntp.google.com!m21no5270979qta.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.java.programmer Date: Thu, 28 Feb 2019 12:11:28 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=50.78.95.177; posting-account=2czF5goAAAD4GBMPIGV4KcD2K4PhoB_H NNTP-Posting-Host: 50.78.95.177 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <8617d379-1659-4582-94fe-6ffb95c975e4@googlegroups.com> Subject: Re: it's Closeable, but I don't want to close() it yet. From: Eric Douglas Injection-Date: Thu, 28 Feb 2019 20:11:29 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Lines: 32 Xref: csiph.com comp.lang.java.programmer:38760 On Wednesday, February 27, 2019 at 10:52:01 AM UTC-5, Andreas Leitgeb wrote= : > In my application there exists an entity that is Closeable. > It is kept in some class, and other parts of my application > request a ref to the entity and do actions on it, then drop > their ref, leaving the entity intact. >=20 > Everything runs fine, except eclipse warns me about spots > where the entity is requested, used, and then dropped. > Eclipse thinks it might need to be close()d. >=20 > Apart from ignoring the warning per eclipse settings or > adding @SuppressWarnings, is there maybe a way to tell > the compiler that a certain ref is not meant to be close()d? > Letting it know that - despite the entity's ultimate fate of > being eventually closed - this is not yet the time&place for it? Here's where it gets ugly. I've had the "Potential resource leak" error ju= st turned off, now I'm trying to turn it on and change code to make it go a= way. I have a class with class level final public static ImageIcon variabl= es. I initialize each ImageIcon in a static block which calls MyClass.clas= s.getResourceAsStream() to open an InputStream to an image file which is pa= ckaged into the jar (so if this ever gets an error you've got serious probl= ems). I then pass that InputStream into another method in a utility class = which uses ImageIO to generate the ImageIcon. That method closes the strea= m. Eclipse complains the stream may be leaked in this class. I take the c= lose out of that class and try to change this to use the try-with-resources= . I previously needed no try statement in this static block. When I wrote= a try-with-resources for a SQL Statement it didn't ask for any exception c= lause, but here it's telling me I need to catch IOException. Now of course= my assignment of the ImageIcon must be inside this try block, and if I add= a catch clause it's complaining it may not be initialized, so I have to in= itialize it to null in the constructor and remove the final.