Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

JNI local-reference accumulation in the string array helpers

未关闭
#1,256 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
75/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
冷清
技术栈
cpp, java
领域
backend

调研方向

首先阅读 dataset/src/main/cpp/jni_util.cc 中的 ToStringVector,以及 dataset/src/main/cpp/jni_wrapper.cc 中的 ToStringMap 和 LoadNamedTables,重点检查每个 GetObjectArrayElement 调用。确保每个获取的局部引用都在正常路径和 JniThrow() 路径上被删除;完成的标准是当前迭代不再有任何被遗弃的引用。

由索引模型根据 Issue 内容生成。

描述

Type: bug
Describe the bug, including details regarding any error messages, version, and platform.

There is a JNI local-reference issue in the dataset helpers that convert Java String[] arrays into C++ containers. They get each element with GetObjectArrayElement() and convert it, but never delete the resulting local reference.

Files:

  • dataset/src/main/cpp/jni_util.cc
  • dataset/src/main/cpp/jni_wrapper.cc

Functions:

  • ToStringVector (jni_util.cc)
  • ToStringMap, LoadNamedTables (jni_wrapper.cc)

Relevant code in ToStringVector():

std::vector<std::string> ToStringVector(JNIEnv* env, jobjectArray& str_array) {
  int length = env->GetArrayLength(str_array);
  std::vector<std::string> vector;
  for (int i = 0; i < length; i++) {
    auto string = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i));
    vector.push_back(JStringToCString(env, string));
  }
  return vector;
}

JStringToCString() does correctly release the native UTF chars it acquires:

const char* chars = env->GetStringUTFChars(string, nullptr);
std::string ret(chars);
env->ReleaseStringUTFChars(string, chars);
return ret;

but that release only matches GetStringUTFChars(). It does not delete the jstring local reference that GetObjectArrayElement() returned, so one reference remains live per element until the native method returns.

ToStringMap() has the same pattern with two references per iteration:

for (int i = 0; i < length; i += 2) {
  auto key = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i));
  auto value = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i + 1));
  map[JStringToCString(env, key)] = JStringToCString(env, value);
}

LoadNamedTables() also takes two per iteration, and has three JniThrow() exits inside the loop body — the odd-length check and the two std::stol failure handlers — so on those paths the references acquired in the current iteration are abandoned mid-loop.

These are local references, so they are reclaimed when the native method returns, and the inputs are option maps, partition columns, and named-table lists — typically tens of entries. There is no observable leak or reachable failure here; the reference count is simply higher than necessary for the duration of the call.

Suggested fix:

auto string = reinterpret_cast<jstring>(env->GetObjectArrayElement(str_array, i));
vector.push_back(JStringToCString(env, string));
env->DeleteLocalRef(string);

For the key/value loops, delete both key and value, including on the JniThrow() paths in LoadNamedTables().

主要语言
Java
星标
95
派生
154
平均合并
2 天 10 小时
30 天内合并 PR
11

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

apache/arrow-java 的其他 Issue

查看 apache/arrow-java 的全部 Issue

相似的 Issue

更多 Java Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。