From ffb40326ff41985904ba74cf83a1b6b71e457626 Mon Sep 17 00:00:00 2001
From: ylecollen <ylecollen@amdocs.com>
Date: Wed, 21 Dec 2016 15:17:53 +0100
Subject: [PATCH] Fix a potential memory leak.

---
 .../olm-sdk/src/main/jni/olm_jni_helper.cpp          | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp
index b52ac30..82c3e28 100644
--- a/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp
+++ b/java/android/OlmLibSdk/olm-sdk/src/main/jni/olm_jni_helper.cpp
@@ -130,8 +130,8 @@ bool setRandomInBuffer(JNIEnv *env, uint8_t **aBuffer2Ptr, size_t aRandomSize)
 jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingClass)
 {
   jlong instanceId = 0;
-  jfieldID instanceIdField;
-  jclass loaderClass;
+  jfieldID instanceIdField = 0;
+  jclass loaderClass = 0;
   jclass requiredClass = 0;
 
   if(NULL!=aJniEnv)
@@ -147,7 +147,6 @@ jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingCl
       if(0 != (instanceIdField=aJniEnv->GetFieldID(loaderClass, "mNativeId", "J")))
       {
         instanceId = aJniEnv->GetLongField(aJavaObject, instanceIdField);
-        aJniEnv->DeleteLocalRef(loaderClass);
         LOGD("## getInstanceId(): read from java instanceId=%lld",instanceId);
       }
       else
@@ -164,7 +163,14 @@ jlong getInstanceId(JNIEnv* aJniEnv, jobject aJavaObject, const char *aCallingCl
   {
     LOGE("## getInstanceId() ERROR! aJniEnv=NULL");
   }
+
   LOGD("## getInstanceId() success - instanceId=%p (jlong)(intptr_t)instanceId=%lld",(void*)instanceId, (jlong)(intptr_t)instanceId);
+
+  if (loaderClass)
+  {
+    aJniEnv->DeleteLocalRef(loaderClass);
+  }
+
   return instanceId;
 }
 
-- 
GitLab