Skip to content

Questions about java and jni code #79

@tes237

Description

@tes237

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;

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions