Attempt to remove non-JNI local reference errors on Android
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 2/5
- Tempo estimado
- 1-3 horas
- Facilidade para iniciantes
- 75/100
- Tipo de issue
- Bug
- Clareza
- Claramente especificada
- Status de atividade
- Pouca atividade
- Stack de tecnologia
- android, swift
- Domínio
- mobile-dev
Direção de pesquisa
Comece em JavaObjectHolder.init(object:environment:), onde as referências jobject recebidas são promovidas e o aviso é acionado. Reproduza as chamadas Android JNI no emulador do Android 15 com o aplicativo FlutterSwift e, em seguida, verifique se os manipuladores frequentes de mensagens do canal de plataforma emitem zero avisos "Attempt to remove non-JNI local reference".
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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
- Linguagem predominante
- Swift
- Estrelas
- 1.3k
- Forks
- 129
- Merge médio
- 7h 16min
- PRs com merge (30d)
- 4
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de swiftlang/swift-java
-
wrap-java: Optional convenience methods can fail to compile when a parameter shadows the method nameAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 70/100
swiftlang/swift-java#945 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 56/100
swiftlang/swift-java#949 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 35/100
swiftlang/swift-java#948 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 4/5 3-5 dias Facilidade para iniciantes 54/100
swiftlang/swift-java#947 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 53/100
swiftlang/swift-java#946 ·
Mantenedores costumam responder em até 1 dia
Todas as issues de swiftlang/swift-java
Issues semelhantes
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
MasterJ93/ATProtoKit#344 ·
-
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 67/100
Mantenedores costumam responder em até 1 dia
-
🎉 Add WoundAberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 77/100
jaywcjlove/awesome-mac#4357 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 86/100
-
Bug: on hover a tab's name in Cra stops ~16 pt short of Arc's, with 24 pt of air before the ✕Abertabug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
VictorKildahl/cra#243 · 1 comentário ·
Mantenedores costumam responder em até 1 dia