Skip to content

JNI local reference leak in GetNativeLogs #32

Open

Description

@QiuYucheng2003

Bug

Injector::GetNativeLogs in app/src/main/jni/Injector/Injector.cpp leaks one JNI local reference per log line.

ret = (jobjectArray)env->NewObjectArray((int)log_messages.size(), env->FindClass("java/lang/String"), env->NewStringUTF(""));
for (size_t i = 0; i < log_messages.size(); ++i) {
    env->SetObjectArrayElement(ret, static_cast<jlong>(i), env->NewStringUTF(log_messages[i].c_str()));
}

NewStringUTF creates a local reference on every iteration. SetObjectArrayElement stores the string in the array and does not free that reference. The references stay until the native method returns.

The loop count is log_messages.size(), and this function never clears the vector, so the count keeps growing. Android's local reference table holds 512 entries by default. A few hundred accumulated log lines can overflow it and abort the process.

Fix
Do not delete ret; it is the return value.

for (size_t i = 0; i < log_messages.size(); ++i) {
    jstring str = env->NewStringUTF(log_messages[i].c_str());
    env->SetObjectArrayElement(ret, static_cast<jsize>(i), str);
    env->DeleteLocalRef(str);
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions