Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Questions about java and jni code #79

Open
tes237 opened this issue Dec 16, 2015 · 0 comments
Open

Questions about java and jni code #79

tes237 opened this issue Dec 16, 2015 · 0 comments

Comments

@tes237
Copy link

tes237 commented Dec 16, 2015

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;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant