JNI local references accumulate during OCR result conversion

Author: QiuYucheng2003Created Sep 7, 2026Updated Sep 7, 2026

Summary

JNI local references are not released while converting OCR results to Java objects.

Affected code: https://github.com/DayBreak-u/chineseocr_lite/blob/54a3df53af823f3bffdde9393d0c021b4f6d94cd/cpp_projects/OcrLiteMnn/src/OcrResultUtils.cpp#L47-L105

Key cases:

  • newJBoxPoint() repeatedly creates jPoint references without releasing them after CallBooleanMethod.
  • getTextBlocks() repeatedly creates jTextBlock references without releasing them after insertion into the list.
  • Temporary references such as clazz, jText, jCharScores, and jBoxPint are also retained until the outer JNI method returns.

C++ helper functions share the caller's JNI local reference frame. Returning from these helpers does not release their local references, and adding an object to a Java list does not consume its JNI reference.

Impact

OCR results containing many text blocks or points can continuously grow the JNI local reference table, potentially causing table overflow, initialization failure, or an application crash.

Suggested fix

Call DeleteLocalRef after each temporary reference is no longer needed, especially jPoint and jTextBlock inside loops. Alternatively, manage each conversion scope with PushLocalFrame / PopLocalFrame.

Source: DayBreak-u/chineseocr_lite