Hi Yu-Hsin,

The most probable cause of this crash is OcResource object going out of scope 
before all of the GET responses have been received by a client side. This patch 
should help to make things a bit more robust: 
https://gerrit.iotivity.org/gerrit/#/c/2306/

In general, I would recommend using ?observe? capability instead of the 
constant polling, unless your server doesn?t support it.

Thanks,
Tim


From: iotivity-dev-bounces at lists.iotivity.org 
[mailto:[email protected]] On Behalf Of Yu-Hsin Hung
Sent: Tuesday, August 18, 2015 6:57 AM
To: iotivity-dev at lists.iotivity.org
Subject: [dev] Android JNI OnEventListener crash

Dear devs,

Recently I found that when Android client sends GET request to server 
frequently (e.g. polling), there will be random crashes due to JNI level being 
aborted. The exception basically says the native code try to access deleted 
weak global reference.

After tracing the implementation, I found that the event listener for 
GET/POST/PUT/? is stored as weak global references, which means they may be 
released by JVM GC on demand and then cause the crashes.

iotivity<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747>/android<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android>/android_api<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api>/base<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base>/jni<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base/jni>/JniOnGetListener.cpp
jobject jListener = env->NewLocalRef(m_jwListener);

In the above code, the m_jwListener is a weak global reference, so it may be 
deleted by GC before this line called. But it seems the ART runtime didn?t 
return NULL as expected, but aborting the execution.

Even if I try to modify them as global reference using NewGlobalRef at the 
constructer of JniOnGetListener (as following), sometimes the crash still 
occurs.

iotivity<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747>/android<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android>/android_api<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api>/base<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base>/jni<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base/jni>/JniOnGetListener.h
jweak m_jwListener;

iotivity<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747>/android<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android>/android_api<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api>/base<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base>/jni<https://github.com/iotivity/iotivity/tree/8c044c478ccafb2faccbff398db02f79e4f58747/android/android_api/base/jni>/JniOnGetListener.cpp
m_jwListener = env->NewWeakGlobalRef(jListener);


The error log is attached.
Does anyone notice this issue? or any idea to fix it? Thanks!



Best Regard,



===

Yu-Hsin Hung,

Intern, Mediatek

Graduate Student, Institute of Computer Science and Engineering,

National Chiao-Tung Universiry, Hsinchu 300, Taiwan

(Phone) +886911012113

(Email) hungys at hotmail.com<mailto:hungys at hotmail.com>

(LinkedIn) https://www.linkedin.com/in/hungys

===
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://lists.iotivity.org/pipermail/iotivity-dev/attachments/20150827/6249dde2/attachment.html>

Reply via email to