As we discussed in Understanding the role of JNI, C++ is designed for manual memory management. It frees memory on command, not on a garbage collector's schedule. If we want to provide a degree of manual memory management to Java users as well, we can write a public method that is responsible for freeing any C++ resources (or other unmanaged resources) associated with a Java class. Conventionally, such a method is named dispose
. Since several of our Filter
implementations will own C++ resources, let's add a dispose
method to Filter.java
:
public interface Filter {
public abstract void dispose();
public abstract void apply(final Mat src, final Mat dst);
}
Let's modify NoneFilter.java
to provide an empty implementation of dispose
, as seen in the following code:
public class NoneFilter implements Filter { @Override public void dispose() { // Do nothing. } @Override public void apply(final Mat src, final Mat dst) { // Do nothing. } }
Similarly...