rspec/rules/S2144/java/rule.adoc

45 lines
1.1 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
import org.eclipse.swt.graphics.Image;
public class MyLeakyView {
Image myImage = new Image("image/path"); // Noncompliant; not disposed
----
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,java]
2021-04-28 16:49:39 +02:00
----
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)
=== Message
Add a "dispose" method call for this "xxx"
endif::env-github,rspecator-view[]