From 9cf7adab492927938ff395e9f104931c1e8dee58 Mon Sep 17 00:00:00 2001 From: Suyeon Hwang Date: Mon, 14 Aug 2017 17:04:51 +0900 Subject: [PATCH] Fix unintended memory free issue Change-Id: I5602f9c7b5594934f3872750a2f9b4b620154ace Signed-off-by: Suyeon Hwang --- .../Interop/Interop.VoiceControlCommand.cs | 2 +- .../Tizen.Uix.VoiceControl/VoiceCommandList.cs | 45 +++++++++++++++++----- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/Tizen.Uix.VoiceControl/Interop/Interop.VoiceControlCommand.cs b/Tizen.Uix.VoiceControl/Interop/Interop.VoiceControlCommand.cs index e9d9bc0..18dd338 100755 --- a/Tizen.Uix.VoiceControl/Interop/Interop.VoiceControlCommand.cs +++ b/Tizen.Uix.VoiceControl/Interop/Interop.VoiceControlCommand.cs @@ -1,4 +1,4 @@ -/* +/* * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved * * Licensed under the Apache License, Version 2.0 (the License); diff --git a/Tizen.Uix.VoiceControl/Tizen.Uix.VoiceControl/VoiceCommandList.cs b/Tizen.Uix.VoiceControl/Tizen.Uix.VoiceControl/VoiceCommandList.cs index 2f78277..3b9d4ca 100755 --- a/Tizen.Uix.VoiceControl/Tizen.Uix.VoiceControl/VoiceCommandList.cs +++ b/Tizen.Uix.VoiceControl/Tizen.Uix.VoiceControl/VoiceCommandList.cs @@ -30,6 +30,7 @@ namespace Tizen.Uix.VoiceControl internal SafeCommandListHandle _handle; private List _list; private VcCmdListCb _callback; + private int _index; /// /// The public constructor. @@ -59,11 +60,29 @@ namespace Tizen.Uix.VoiceControl throw ExceptionFactory.CreateException(error); } _handle = handle; + _list = new List(); + _index = 0; } internal VoiceCommandList(SafeCommandListHandle handle) { _handle = handle; + _index = 0; + + _list = new List(); + _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); + } } /// @@ -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); } /// @@ -180,6 +201,8 @@ namespace Tizen.Uix.VoiceControl Log.Error(LogTag, "Remove Failed with error " + error); throw ExceptionFactory.CreateException(error); } + + _list.Remove(command); } /// @@ -200,12 +223,12 @@ namespace Tizen.Uix.VoiceControl /// This exception can be due to not supported. public IEnumerable GetAllCommands() { - _list = new List(); + List commandList = new List(); _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; } /// @@ -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; } /// @@ -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++; } /// @@ -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--; } } } -- 2.7.4