rspec/rules/S2144/java/rule.adoc
2022-02-04 16:28:24 +00:00

40 lines
1.1 KiB
Plaintext

Java's garbage collection cannot be relied on to clean up everything. Specifically, subclasses of ``++org.eclipse.swt.graphics.Resource++`` must be manually ``++dispose()++``-ed when you're done with them.
Particularly for the ``++Image++`` subclass, which retains an open ``++FileHandle++`` for the life of the instance, failure to properly ``++dispose++`` of ``++Resource++``s can result in a resource leak which could bring first the application and then perhaps the box it's on to their knees.
== Noncompliant Code Example
[source,java]
----
import org.eclipse.swt.graphics.Image;
public class MyLeakyView {
Image myImage = new Image("image/path"); // Noncompliant; not disposed
----
== Compliant Solution
[source,java]
----
import org.eclipse.swt.graphics.Image;
public class MyView {
Image myImage = new Image("image/path");
public void callMeWhenItsDone() {
myImage.dispose();
}
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
include::message.adoc[]
endif::env-github,rspecator-view[]