libmx3 / mx3

a sample project showcasing/collecting cross platform techniques on mobile
MIT License
1.17k stars 146 forks source link

Questions about java and jni code #79

Open tes237 opened 8 years ago

tes237 commented 8 years ago

Currently i am trying to develop crossplatform mobile library with visual studio 2015. I am investigating your sources. My question is that following function makes shared_ptr map and creates CppProxy of java from jni, But why do you make it WeakReference. what happen you just do jniEnv->NewObject alone? Android Java new does not work as WeakReference? It is rather Java question than jni.

/static/ jobject JniCppProxyCache::get(const std::shared_ptr & cppObj, JNIEnv * jniEnv, const CppProxyClassInfo & proxyClass, jobject (factory)(const std::shared_ptr &, JNIEnv , const CppProxyClassInfo &)) { CppProxyCacheState & st = CppProxyCacheState::get(); const std::lock_guardstd::mutex lock(st.mtx);

auto it = st.m.find(cppObj.get());
if (it != st.m.end()) 
{
    // It's in the map. See if the WeakReference still points to an object.
    if (jobject javaObj = it->second.get(jniEnv)) 
    {
        return javaObj;
    } 
    else 
    {
        // The WeakReference is expired, so prune it from the map eagerly.
        st.m.erase(it);
    }
}

jobject wrapper = factory(cppObj, jniEnv, proxyClass);

/* Make a Java WeakRef object */
st.m.emplace(cppObj.get(), JavaWeakRef(jniEnv, wrapper));

return wrapper;

}