Fix unintended memory free issue 07/144007/7 accepted/tizen_4.0_unified accepted/tizen_unified tizen_4.0 accepted/tizen/4.0/unified/20170828.223331 accepted/tizen/unified/20170818.083349 submit/tizen/20170817.073026 submit/tizen/20170817.102851 submit/tizen_4.0/20170828.100003
authorSuyeon Hwang <stom.hwang@samsung.com>
Mon, 14 Aug 2017 08:04:51 +0000 (17:04 +0900)
committerSuyeon Hwang <stom.hwang@samsung.com>
Thu, 17 Aug 2017 02:05:43 +0000 (11:05 +0900)
Change-Id: I5602f9c7b5594934f3872750a2f9b4b620154ace
Signed-off-by: Suyeon Hwang <stom.hwang@samsung.com>
Tizen.Uix.VoiceControl/Interop/Interop.VoiceControlCommand.cs
Tizen.Uix.VoiceControl/Tizen.Uix.VoiceControl/VoiceCommandList.cs

index e9d9bc0..18dd338 100755 (executable)
@@ -1,4 +1,4 @@
-/*
+/*
 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
index 2f78277..3b9d4ca 100755 (executable)
@@ -30,6 +30,7 @@ namespace Tizen.Uix.VoiceControl
         internal SafeCommandListHandle _handle;
         private List<VoiceCommand> _list;
         private VcCmdListCb _callback;
+        private int _index;
 
         /// <summary>
         /// The public constructor.
@@ -59,11 +60,29 @@ namespace Tizen.Uix.VoiceControl
                 throw ExceptionFactory.CreateException(error);
             }
             _handle = handle;
+            _list = new List<VoiceCommand>();
+            _index = 0;
         }
 
         internal VoiceCommandList(SafeCommandListHandle handle)
         {
             _handle = handle;
+            _index = 0;
+
+            _list = new List<VoiceCommand>();
+            _callback = (IntPtr vcCommand, IntPtr userData) =>
+            {
+                SafeCommandHandle cmdHandle = new SafeCommandHandle(vcCommand);
+                cmdHandle._ownership = false;
+                _list.Add(new VoiceCommand(cmdHandle));
+                return true;
+            };
+            ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
+            if (error != ErrorCode.None)
+            {
+                Log.Error(LogTag, "GetAllCommands Failed with error " + error);
+                throw ExceptionFactory.CreateException(error);
+            }
         }
 
         /// <summary>
@@ -116,13 +135,13 @@ namespace Tizen.Uix.VoiceControl
             {
                 SafeCommandHandle current;
                 ErrorCode error = VcCmdListGetCurrent(_handle, out current);
-                if (error != ErrorCode.None)
+                if (ErrorCode.None != error)
                 {
                     Log.Error(LogTag, "Current Failed with error " + error);
                     return null;
                 }
-                current._ownership = false;
-                return new VoiceCommand(current);
+
+                return _list[_index];
             }
         }
 
@@ -152,6 +171,8 @@ namespace Tizen.Uix.VoiceControl
                 Log.Error(LogTag, "Add Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+
+            _list.Add(command);
         }
 
         /// <summary>
@@ -180,6 +201,8 @@ namespace Tizen.Uix.VoiceControl
                 Log.Error(LogTag, "Remove Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+
+            _list.Remove(command);
         }
 
         /// <summary>
@@ -200,12 +223,12 @@ namespace Tizen.Uix.VoiceControl
         /// <exception cref="NotSupportedException">This exception can be due to not supported.</exception>
         public IEnumerable<VoiceCommand> GetAllCommands()
         {
-            _list = new List<VoiceCommand>();
+            List<VoiceCommand> commandList = new List<VoiceCommand>();
             _callback = (IntPtr vcCommand, IntPtr userData) =>
             {
                 SafeCommandHandle handle = new SafeCommandHandle(vcCommand);
                 handle._ownership = false;
-                _list.Add(new VoiceCommand(handle));
+                commandList.Add(new VoiceCommand(handle));
                 return true;
             };
             ErrorCode error = VcCmdListForeachCommands(_handle, _callback, IntPtr.Zero);
@@ -238,11 +261,12 @@ namespace Tizen.Uix.VoiceControl
         public void First()
         {
             ErrorCode error = VcCmdListFirst(_handle);
-            if (error != ErrorCode.None)
+            if (ErrorCode.None != error)
             {
                 Log.Error(LogTag, "First Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+            _index = 0;
         }
 
         /// <summary>
@@ -265,11 +289,12 @@ namespace Tizen.Uix.VoiceControl
         public void Last()
         {
             ErrorCode error = VcCmdListLast(_handle);
-            if (error != ErrorCode.None)
+            if (ErrorCode.None != error)
             {
                 Log.Error(LogTag, "Last Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+            _index = Count - 1;
         }
 
         /// <summary>
@@ -296,11 +321,12 @@ namespace Tizen.Uix.VoiceControl
         public void Next()
         {
             ErrorCode error = VcCmdListNext(_handle);
-            if (error != ErrorCode.None)
+            if (ErrorCode.None != error)
             {
                 Log.Error(LogTag, "Next Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+           _index++;
         }
 
         /// <summary>
@@ -327,11 +353,12 @@ namespace Tizen.Uix.VoiceControl
         public void Previous()
         {
             ErrorCode error = VcCmdListPrev(_handle);
-            if (error != ErrorCode.None)
+            if (ErrorCode.None != error)
             {
                 Log.Error(LogTag, "Previous Failed with error " + error);
                 throw ExceptionFactory.CreateException(error);
             }
+            _index--;
         }
     }
 }