Haseeb Pavaratty
Haseeb Pavaratty

Reputation: 683

E/dalvikvm: JNI ERROR (app bug): local reference table overflow (max=512)

I made an app that simply read data with NFC and writes back the data after modification. After tapping card for about 100 times, the newIntent is not being registered and I'm getting this in logcat

01-01 05:43:46.990 6347-6376/? E/dalvikvm: JNI ERROR (app bug): local reference table overflow (max=512)
01-01 05:43:46.990 6347-6376/? E/dalvikvm: Failed adding to JNI local ref table (has 512 entries)
01-01 05:43:46.990 6347-6376/? E/dalvikvm: VM aborting
01-01 05:43:46.990 6347-6376/? A/libc: Fatal signal 11 (SIGSEGV) at 0xdeadd00d (code=1), thread 6376 (message)
01-01 05:43:48.010 9950-9950/com.android.nfc E/Trace: error opening trace file: No such file or directory (2)
01-01 05:43:49.930 9950-9971/com.android.nfc E/NFC-HCI: Could not open /system/vendor/firmware/libpn544_fw.so

Is it something related to dalvikvm? Because I'm not getting this error in my new Phone which is running android 9.1. The issue is seen only in the device running 4.2 so far.

I'm using the default NFC library and there is no native code. Previously I was using the same code in a project with native code and I got the same error. Many answers here in Stack overflow suggest that it might be due to native code where we are not clearing local reference, so I tried only the NFC operation in a separate project without any other code. Still getting the same error.

Upvotes: 0

Views: 136

Answers (1)

Andrew
Andrew

Reputation: 10252

My thoughts is that because you don't seem to be using nfcAdapter.enableForegroundDispatch quite correctly you are running it out of references.

You are enabling ForegroundDispatch in onResume and when a card is detected you App is paused and then resumed again where it adds a second reference as the first was never disposed of as it should be in the onPause method of your App.

The Docs For enableForegroundDispatch From https://developer.android.com/reference/android/nfc/NfcAdapter#enableForegroundDispatch(android.app.Activity,%20android.app.PendingIntent,%20android.content.IntentFilter[],%20java.lang.String[][])

Say

This method must be called from the main thread, and only when the activity is in the foreground (resumed). Also, activities must call disableForegroundDispatch(Activity) before the completion of their Activity#onPause callback to disable foreground dispatch after it has been enabled.

Your code never calls disableForegroundDispatch(Activity) so add the method to MainActivity

@Override
protected void onPause() {
   nfcAdapter.disableForegroundDispatch(this);
}

Or use the better enableReaderMode API https://developer.android.com/reference/android/nfc/NfcAdapter#enableReaderMode(android.app.Activity,%20android.nfc.NfcAdapter.ReaderCallback,%20int,%20android.os.Bundle)

Upvotes: 0

Related Questions