Skip to content

Commit

Permalink
Use WeakGlobalRef for android MapController instance
Browse files Browse the repository at this point in the history
- MapController is calling dispose, so it must be alive here
  • Loading branch information
hjanetzek committed Nov 30, 2018
1 parent bf23539 commit b4e5eff
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 17 deletions.
6 changes: 1 addition & 5 deletions platforms/android/tangram/src/main/cpp/androidPlatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ std::string AndroidPlatform::fontPath(const std::string& _family, const std::str
AndroidPlatform::AndroidPlatform(JNIEnv* _jniEnv, jobject _assetManager, jobject _tangramInstance)
: m_jniWorker(jvm) {

m_tangramInstance = _jniEnv->NewGlobalRef(_tangramInstance);
m_tangramInstance = _jniEnv->NewWeakGlobalRef(_tangramInstance);

m_assetManager = AAssetManager_fromJava(_jniEnv, _assetManager);

Expand All @@ -193,10 +193,6 @@ AndroidPlatform::AndroidPlatform(JNIEnv* _jniEnv, jobject _assetManager, jobject
sqlite3_ndk_init(m_assetManager);
}

void AndroidPlatform::dispose(JNIEnv* _jniEnv) {
_jniEnv->DeleteGlobalRef(m_tangramInstance);
}

void AndroidPlatform::requestRender() const {
m_jniWorker.enqueue([&](JNIEnv *jniEnv) {
jniEnv->CallVoidMethod(m_tangramInstance, requestRenderMethodID);
Expand Down
1 change: 0 additions & 1 deletion platforms/android/tangram/src/main/cpp/androidPlatform.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ class AndroidPlatform : public Platform {
public:

AndroidPlatform(JNIEnv* _jniEnv, jobject _assetManager, jobject _tangramInstance);
void dispose(JNIEnv* _jniEnv);
void shutdown() override {}
void requestRender() const override;
void setContinuousRendering(bool _isContinuous) override;
Expand Down
11 changes: 2 additions & 9 deletions platforms/android/tangram/src/main/cpp/jniExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,16 +144,9 @@ extern "C" {
return reinterpret_cast<jlong>(map);
}

JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeDispose(JNIEnv* jniEnv, jobject obj, jlong mapPtr) {
JNIEXPORT void JNICALL Java_com_mapzen_tangram_MapController_nativeDispose(JNIEnv* jniEnv, jobject tangramInstance, jlong mapPtr) {
assert(mapPtr > 0);
auto map = reinterpret_cast<Tangram::Map*>(mapPtr);
// Don't dispose MapController ref before map is teared down,
// delete map or worker threads might call back to it (e.g. requestRender)
auto platform = map->getPlatform();

delete map;

static_cast<Tangram::AndroidPlatform&>(*platform).dispose(jniEnv);
delete reinterpret_cast<Tangram::Map*>(mapPtr);
}

JNIEXPORT jint JNICALL Java_com_mapzen_tangram_MapController_nativeLoadScene(JNIEnv* jniEnv, jobject obj, jlong mapPtr, jstring path, jobjectArray updateStrings) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ protected MapController(@NonNull Context context) {
fontFileParser = new FontFileParser();
fontFileParser.parse();

mapPointer = nativeInit(this, assetManager);
mapPointer = nativeInit(assetManager);
if (mapPointer <= 0) {
throw new RuntimeException("Unable to create a native Map object! There may be insufficient memory available.");
}
Expand Down Expand Up @@ -1257,7 +1257,7 @@ boolean setMarkerDrawOrder(final long markerId, final int drawOrder) {
// ==============

private synchronized native void nativeOnLowMemory(long mapPtr);
private synchronized native long nativeInit(MapController instance, AssetManager assetManager);
private synchronized native long nativeInit(AssetManager assetManager);
private synchronized native void nativeDispose(long mapPtr);
private synchronized native int nativeLoadScene(long mapPtr, String path, String[] updateStrings);
private synchronized native int nativeLoadSceneAsync(long mapPtr, String path, String[] updateStrings);
Expand Down

0 comments on commit b4e5eff

Please sign in to comment.