[NoACR][MachineLearning.Inference] Fix bug using wrong handle
[platform/core/csapi/tizenfx.git] / src / Tizen.MachineLearning.Inference / Tizen.MachineLearning.Inference / TensorsData.cs
index 7ab6542..8440155 100755 (executable)
@@ -1,5 +1,5 @@
 /*
-* Copyright (c) 2019 Samsung Electronics Co., Ltd All Rights Reserved
+* Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
@@ -34,21 +34,23 @@ namespace Tizen.MachineLearning.Inference
         /// Creates a TensorsData instance with handle which is given by TensorsInfo.
         /// </summary>
         /// <param name="handle">The handle of tensors data.</param>
-        /// <param name="info">The handle of tensors info. (Default: null)</param>
+        /// <param name="info">The handle of tensors info.</param>
         /// <param name="isFetch">The boolean value for fetching the data (Default: false)</param>
+        /// <param name="hasOwnership">The boolean value for automatic disposal (Default: true)</param>
         /// <since_tizen> 6 </since_tizen>
-        private TensorsData(IntPtr handle, TensorsInfo info, bool isFetch)
+        private TensorsData(IntPtr handle, TensorsInfo info, bool isFetch = false, bool hasOwnership = true)
         {
             NNStreamer.CheckNNStreamerSupport();
             NNStreamerError ret = NNStreamerError.None;
 
             /* Set internal object */
             _handle = handle;
-            _tensorsInfo = info;
+            /* Because developers can change the TensorsInfo object, it should be stored as a deep-copied instance. */
+            _tensorsInfo = info.Clone();
 
             /* Set count */
             int count = 0;
-            ret = Interop.Util.GetTensorsCount(_handle, out count);
+            ret = Interop.Util.GetTensorsCount(_tensorsInfo.GetTensorsInfoHandle(), out count);
             NNStreamer.CheckException(ret, "unable to get the count of TensorsData");
 
             _dataList = new ArrayList(count);
@@ -78,6 +80,9 @@ namespace Tizen.MachineLearning.Inference
                     _dataList.Add(bufData);
                 }
             }
+
+            /* If it created as DataReceivedEventArgs, do not dispose. */
+            _disposed = !hasOwnership;
         }
 
         /// <summary>
@@ -198,6 +203,8 @@ namespace Tizen.MachineLearning.Inference
             if (disposing)
             {
                 // release managed object
+                _tensorsInfo.Dispose();
+                _tensorsInfo = null;
             }
 
             // release unmanaged objects
@@ -231,21 +238,16 @@ namespace Tizen.MachineLearning.Inference
             }
         }
 
-        internal static TensorsData CreateFromNativeHandle(IntPtr dataHandle, IntPtr infoHandle, bool isFetch)
+        internal static TensorsData CreateFromNativeHandle(IntPtr dataHandle, IntPtr infoHandle, bool isFetch = false, bool hasOwnership = true)
         {
-            TensorsData retTensorsData = null;
+            TensorsInfo info = null;
 
-            if (infoHandle == IntPtr.Zero)
-            {
-                retTensorsData = new TensorsData(dataHandle, null, isFetch);
-            }
-            else
+            if (infoHandle != IntPtr.Zero)
             {
-                TensorsInfo info = TensorsInfo.ConvertTensorsInfoFromHandle(infoHandle);
-                retTensorsData = new TensorsData(dataHandle, info, isFetch);
+                info = TensorsInfo.ConvertTensorsInfoFromHandle(infoHandle);
             }
 
-            return retTensorsData;
+            return new TensorsData(dataHandle, info, isFetch, hasOwnership);
         }
 
         private void CheckIndex(int index)