Files
openide/java/java-impl/resources/inspectionDescriptions/CapturingCleaner.html
2025-02-05 04:43:28 +00:00

25 lines
1.0 KiB
HTML

<html>
<body>
Reports <code>Runnable</code> passed to a <code>Cleaner.register()</code> capturing reference being registered.
If the reference is captured, it will never be phantom reachable and the cleaning action will never be invoked.
<p>Possible sources of this problem:</p>
<ul>
<li>Lambda using non-static methods, fields, or <code>this</code> itself</li>
<li>Non-static inner class (anonymous or not) always captures this reference in java up to 18 version</li>
<li>Instance method reference</li>
<li>Access to outer class non-static members from non-static inner class</li>
</ul>
<!-- tooltip end -->
<p>Sample of code that will be reported: </p>
<pre><code>
int fileDescriptor;
Cleaner.Cleanable cleanable = Cleaner.create().register(this, () -> {
System.out.println("adsad");
//<i>this</i> is captured via <i>fileDescriptor</i>
fileDescriptor = 0;
});
</code></pre>
<p>This inspection only reports if the language level of the project or module is 9 or higher.</p>
<p><small>New in 2018.1</small></p>
</body>
</html>