Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader02.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Andreas Leitgeb Newsgroups: comp.lang.java.programmer Subject: Re: it's Closeable, but I don't want to close() it yet. Date: Tue, 5 Mar 2019 14:34:32 -0000 (UTC) Organization: A noiseless patient Spider Lines: 62 Message-ID: References: <709a1f3b-fc99-4bd0-a8b8-866092bb7ae9@googlegroups.com> <8e1597a2-5c18-4fd1-83a4-7667e4d11e77@googlegroups.com> <23a8128b-5c54-45ba-bfd0-2f7dda078517@googlegroups.com> Reply-To: avl@logic.at Injection-Date: Tue, 5 Mar 2019 14:34:32 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="8313cfdc85cc307cb004a8c986908a47"; logging-data="3129"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19khbCj95e4qjWpmRkKhzs1" User-Agent: slrn/1.0.3 (Linux) Cancel-Lock: sha1:r3eBmF4DhjnbrnFwIMMn1JoBRSY= Xref: csiph.com comp.lang.java.programmer:38792 Daniele Futtorovic wrote: > On 2019-03-04 10:38, Andreas Leitgeb wrote: >> Daniele Futtorovic wrote: >>> On 2019-03-01 10:31, Andreas Leitgeb wrote: >>>> private static void helper2(Context ctx) { >>>> Entity ent = ctx.getEnt(); >>>> if (ent instanceof EntityImpl) { >>>> // the following line gets tagged by eclipse: >>>> // "Resource leak: 'entImp' is never closed" >>>> EntityImpl entImp = (EntityImpl)ent; >>>> entImp.b(); >>>> } >>>> } >>>> } >>> Colour me agnostic, but that warning seems absolutely ridiculous to me. >>> Are you sure that's the cause of it? Could EntityImpl#b perhaps be doing >>> something funky? >> I did provide the implementation of b() in my posting. It was an SSCCE. >> (Reminder: It was empty, except for a /* ... */ comment.) > Hmmm. > For the record, IntelliJ fires off no such warning (then again, its > inspection profiles are configurable). > Do you also get the warning if you used a try-with-resources in the main > of your SSCCE? Yes. I tried it. The warning is only relevant to helper2, and it doesn't matter what main(String[] args) looks like. In case you want to see what it looks like in eclipse: https://pasteboard.co/I41cNSn.png (*) For what it's worth: I added a third helper (starting as a copy of helper2) which receives an Entity-typed parameter instead of the Context, and its first line changed to Entity ent = e; then I still get the warning, but if the initialization of entImpl is also changed to init from "e" rather than from "ent", then the warning disappears. private static void helper3(Entity e) { Entity ent = e; if (ent instanceof EntityImpl) { // the following line gets tagged by eclipse: // "Resource leak: 'entImp' is never closed" EntityImpl entImp = (EntityImpl)ent; // replacing above line to this lets warning disappear //EntityImpl entImp = (EntityImpl)e; entImp.b(); } } Apparently, eclipse's heuristic does take the immediate initialisation source of the Closeable ref into account. (*): got this site from google. Reviewing the link didn't work for me, but maybe does for others. if it doesn't work, and you're curious, feel free to suggest an alternative image upload site.