RFR 9: 8138696 : java.lang.ref.Cleaner - an easy to use alternative to finalization

Chris Hegarty chris.hegarty at oracle.com
Wed Dec 9 15:03:46 UTC 2015


Peter,

On 09/12/15 07:05, Peter Levart wrote:
> Hi,
>
> I think the only way to try to prevent such things is with a good
> example in javadoc that "screams" of possible miss-usages.
>
>
> public static class CleanerExample implements AutoCloseable {
>
>          private static final Cleaner cleaner = ...; // preferably a
> shared cleaner
>
>          private final PrivateNativeResource pnr;
>
>          private final Cleaner.Cleanable cleanable;
>
>          public CleanerExample(args, ...) {
>
>              // prepare captured state as local vars...
>              PrivateNativeResource _pnr = ...;
>
>              this.cleanable = cleaner.register(this, () -> {
>                  // DON'T capture any instance fields with lambda since
> that would
>                  // capture 'this' and prevent it from becoming

I assume that the WARNING should include anonymous inner classes too
( which I expect are quite common, though less now with lambda ) ?

Is "leaking" 'this' in a constructor a potential issue with respect
to the visibility of pnr? As well as causing red-squiggly lines in
the IDE ;-)

-Chris.


> phantom-reachable!!!
>                  _pnr.close();
>              });
>
>              this.pnr = _pnr;
>          }
>
>          public void close() {
>              cleanable.clean();
>          }
>
>
> Regards, Peter
>



More information about the core-libs-dev mailing list