Skip to content

Commit 210558e

Browse files
committed
[Java.Interop] Improve "Deleting JNI local reference" message.
Provide the current Thread.ManagedThreadId value in the "Deleting JNI local reference...from wrong thread" message, along with the current stack trace of the deletion. Write the "Deleting JNI local reference" messages to IJniHandleManager.WriteLocalReferenceLine(), so that the messages don't "spam" stdout and are instead "in context" for LREF messages.
1 parent d43488c commit 210558e

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

src/Java.Interop/Java.Interop/JniEnvironment.cs

+13-3
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,21 @@ internal void LogCreateLocalRef (JniLocalReference value)
220220

221221
internal void DeleteLocalReference (JniLocalReference value, IntPtr handle)
222222
{
223-
if (lrefs == null || !lrefs.Contains (value)) {
224-
Debug.WriteLine ("Deleting JNI local reference handle 0x{0} from wrong thread! Ignoring...", handle.ToString ("x"));
223+
var c = current;
224+
for ( ; c != null; c = c.previous) {
225+
if (c.lrefs == null || !c.lrefs.Contains (value))
226+
continue;
227+
break;
228+
}
229+
if (c == null) {
230+
JavaVM.JniHandleManager.WriteLocalReferenceLine (
231+
"Deleting JNI local reference handle 0x{0} from wrong thread id={1}! Ignoring...",
232+
handle.ToString ("x"), Thread.CurrentThread.ManagedThreadId);
233+
JavaVM.JniHandleManager.WriteLocalReferenceLine ("{0}",
234+
System.Activator.CreateInstance (Type.GetType ("System.Diagnostics.StackTrace")));
225235
return;
226236
}
227-
lrefs.Remove (value);
237+
c.lrefs.Remove (value);
228238
JniEnvironment.Current.JavaVM.JniHandleManager.DeleteLocalReference (this, handle);
229239
}
230240

0 commit comments

Comments
 (0)