Skip to content

Commit

Permalink
Remove java finalizer from Box, provide refCount instead
Browse files Browse the repository at this point in the history
This makes it consistent with Pix implementation, where finalizer was also removed in rmtheis/tess-two@44627d8

This removes warning "Box was not terminated using recycle()" which was wrongly raised in testPixaReplacePix() test (see rmtheis/tess-two#159 (comment)), but now requires that user don't forget to release his Box instances (same as with Pix).
  • Loading branch information
Robyer committed Sep 17, 2019
1 parent 896f8b4 commit dff02fb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
7 changes: 7 additions & 0 deletions tesseract4android/src/main/cpp/leptonica/box.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ jlong Java_com_googlecode_leptonica_android_Box_nativeCreate(JNIEnv *env, jclass
return (jlong) box;
}

jint Java_com_googlecode_leptonica_android_Box_nativeGetRefCount(JNIEnv *env, jclass clazz,
jlong nativeBox) {
BOX *box = (BOX *) nativeBox;

return (jint) boxGetRefcount(box);
}

void Java_com_googlecode_leptonica_android_Box_nativeDestroy(JNIEnv *env, jclass clazz,
jlong nativeBox) {
BOX *box = (BOX *) nativeBox;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ public boolean getGeometry(@Size(min = 4) int[] geometry) {
return nativeGetGeometry(mNativeBox, geometry);
}

public int getRefCount() {
return nativeGetRefCount(mNativeBox);
}

/**
* Releases resources and frees any memory associated with this Box.
*/
Expand All @@ -219,18 +223,6 @@ public void recycle() {
}
}

@Override
protected void finalize() throws Throwable {
try {
if (!mRecycled) {
Log.w(TAG, "Box was not terminated using recycle()");
recycle();
}
} finally {
super.finalize();
}
}

// ***************
// * NATIVE CODE *
// ***************
Expand All @@ -245,6 +237,8 @@ protected void finalize() throws Throwable {

private static native int nativeGetHeight(long nativeBox);

private static native int nativeGetRefCount(long nativePix);

private static native void nativeDestroy(long nativeBox);

private static native boolean nativeGetGeometry(long nativeBox, int[] geometry);
Expand Down

0 comments on commit dff02fb

Please sign in to comment.