Keep the added peer instance
authorSung-jae Park <nicesj.park@samsung.com>
Tue, 26 Oct 2021 00:38:46 +0000 (09:38 +0900)
committerYoungjae Shin <yj99.shin@samsung.com>
Thu, 28 Oct 2021 10:50:57 +0000 (19:50 +0900)
[Problem] Peer instance should not be collected by the GC
[Solution] Keep the peer instance if it is added to the inference handler

Change-Id: I74079cd68d3dba6047fea0e024585ff8795b364e
Signed-off-by: Sung-jae Park <nicesj.park@samsung.com>
subprojects/libbeyond-android/src/main/java/com/samsung/android/beyond/inference/InferenceHandler.java

index 7f22e086e257717018efc78729827bd81d20aac3..2e828d5e5025e387517f1df84ede11b655e106e7 100644 (file)
@@ -16,6 +16,7 @@
 
 package com.samsung.android.beyond.inference;
 
+import java.util.*;
 import android.util.Log;
 
 import com.samsung.android.beyond.inference.tensor.TensorSet;
@@ -26,6 +27,7 @@ import static com.samsung.android.beyond.inference.Option.TAG;
 import androidx.annotation.NonNull;
 
 public class InferenceHandler extends NativeInstance {
+    private List<Peer> peerList = new ArrayList<Peer>();
 
     private TensorOutputCallback tensorOutputCallback;
 
@@ -38,6 +40,8 @@ public class InferenceHandler extends NativeInstance {
         registerNativeInstance(nativeInstance, (Long instance) -> destroy(instance));
     }
 
+    // TODO:
+    // removePeer() should be provided
     public boolean addInferencePeer(@NonNull Peer inferencePeer) {
         if (instance == 0L) {
             Log.e(TAG, "Instance is invalid.");
@@ -49,7 +53,14 @@ public class InferenceHandler extends NativeInstance {
             return false;
         }
 
-        return addPeer(instance, inferencePeer.getNativeInstance());
+        if (addPeer(instance, inferencePeer.getNativeInstance()) == true) {
+            // NOTE:
+            // Hold the peer instance until release them from the this.
+            peerList.add(inferencePeer);
+            return true;
+        }
+
+        return false;
     }
 
     public boolean loadModel(@NonNull String modelPath) {