Attempt to remove non-JNI local reference errors on Android
Los mantenedores suelen responder en 1 día
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 2/5
- Tiempo estimado
- 1-3 horas
- Aptitud para principiantes
- 75/100
- Tipo de issue
- Error
- Claridad
- Bien especificado
- Estado de actividad
- Tranquilo
- Stack tecnológico
- android, swift
- Área
- mobile-dev
Línea de trabajo
Comienza en JavaObjectHolder.init(object:environment:), donde las referencias jobject entrantes se promueven y se activa la advertencia. Reproduce las llamadas de Android JNI en el emulador de Android 15 con la aplicación FlutterSwift y verifica después que los controladores frecuentes de mensajes del canal de plataforma emitan cero advertencias "Attempt to remove non-JNI local reference".
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Using FlutterSwift with the latest swift-java and Android SDK, I see
2026-04-01 15:37:27.149 7457-7457 ktone.InfernoUI com.lukktone.InfernoUI W Attempt to remove non-JNI local reference
in the Android logs.
Proposed fix is to remove DeleteLocalRef on incoming references in JavaObjectHolder.init to fix Android JNI warnings. (This is by @ampcode-com, I haven't had a chance to review, I hate AI-generated PRs as much as the next person but I haven't had the cycles to look into the root cause myself yet.)
Root Cause
JavaObjectHolder.init(object:environment:) promotes the incoming jobject to a global reference via NewGlobalRef, then checks if the original is a local reference and calls DeleteLocalRef on it. This is problematic because:
-
JNI parameter references are not owned by native code. When Java calls a native method, the
jobjectparameters are local references managed by the JNI frame. The JNI specification states these are automatically cleaned up when the native method returns. Native code should not callDeleteLocalRefon them. -
ART's CheckJNI detects this. Android's runtime (ART) has CheckJNI enabled by default, which distinguishes between locally-created references (via
FindClass,NewObject, etc.) and parameter references. CallingDeleteLocalRefon a parameter reference triggers the warning. -
GetObjectRefTypereturnsJNILocalRefTypefor both cases, so the existing check cannot distinguish parameter references from locally-created references.
Fix
Remove the DeleteLocalRef call entirely. After NewGlobalRef creates a new strong reference to the Java object, the original local reference (whether a parameter or locally-created) will be cleaned up automatically when the JNI frame is popped. This is safe and correct per the JNI specification:
"Local references are valid for the duration of a native method call. They are freed automatically after the native method returns." — JNI Specification
The only scenario where explicit DeleteLocalRef is needed is to avoid local reference table overflow when creating many local references in a loop within a single native method call. JavaObjectHolder.init does not fall into this category.
Testing
Tested on Android 15 (API 36) emulator with a FlutterSwift application that makes frequent JNI calls (platform channel message handlers). Before the fix, every message handler invocation produced 1–3 "Attempt to remove non-JNI local reference" warnings. After the fix, zero warnings are emitted
- Lenguaje dominante
- Swift
- Estrellas
- 1.3k
- Forks
- 127
- Merge medio
- 2 d 14 h
- PR fusionados (30 d)
- 5
Preparar el entorno
- Sin Dockerfile ni archivo de Docker Compose
- Sin plantilla de pull request
- Leer la guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de swiftlang/swift-java
-
wrap-java: Optional convenience methods can fail to compile when a parameter shadows the method nameAbierto
Dificultad 2/5 1-3 horas Aptitud para principiantes 70/100
swiftlang/swift-java#945 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 56/100
swiftlang/swift-java#949 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 5/5 Más de una semana Aptitud para principiantes 35/100
swiftlang/swift-java#948 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 4/5 3-5 días Aptitud para principiantes 54/100
swiftlang/swift-java#947 ·
Los mantenedores suelen responder en 1 día
-
Dificultad 3/5 1-2 días Aptitud para principiantes 53/100
swiftlang/swift-java#946 ·
Los mantenedores suelen responder en 1 día
Todos los issues de swiftlang/swift-java
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
VictorKildahl/cra#192 ·
Los mantenedores suelen responder en 1 día
-
area:docs phase:release priority:p0 type:test
Dificultad 2/5 1-3 horas Aptitud para principiantes 66/100
-
[Type] Bug Accessibility Notifications
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
wordpress-mobile/WordPress-iOS#26340 · 1 comentario ·
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 65/100
Los mantenedores suelen responder en 1 día
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
Los mantenedores suelen responder en 1 día