[Tizen.Core] Fix memory leak (#6336)
authorhjhun <36876573+hjhun@users.noreply.github.com>
Fri, 20 Sep 2024 04:04:19 +0000 (13:04 +0900)
committerGitHub <noreply@github.com>
Fri, 20 Sep 2024 04:04:19 +0000 (13:04 +0900)
The problem is that if the setter of ChannelObject's Data Property is called
twice in a row, the previous data will not be removed. To prevent this,
this patch is made it so that when the setter is called, the previous data
is erased.

Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/Tizen.Core/Tizen.Core/ChannelObject.cs

index 98730fa19527e6c1788729088514f0ade23a9776..cc967effb7f9b74c73b59a274b70db70c6614256 100644 (file)
@@ -17,6 +17,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Runtime.InteropServices;
+using System.Security.Cryptography;
 
 namespace Tizen.Core
 {
@@ -30,7 +31,7 @@ namespace Tizen.Core
         private bool _disposed = false;
         private static readonly ConcurrentDictionary<int, object> _dataMap = new ConcurrentDictionary<int, object>();
         private static readonly object _dataLock = new object();
-        private static int _dataId = 0;
+        private static int _dataId = 1;
 
         /// <summary>
         /// Constructor for creating a new channel object with specified ID and data.
@@ -109,8 +110,19 @@ namespace Tizen.Core
             set
             {
                 int id;
+                Interop.LibTizenCore.TizenCoreChannel.ObjectGetData(_handle, out IntPtr handle);
+                if (handle != IntPtr.Zero)
+                {
+                    id = (int)handle;
+                    _dataMap.TryRemove(id, out var _);
+                }
+
                 lock (_dataLock)
                 {
+                    if (_dataId + 1 < 0)
+                    {
+                        _dataId = 1;
+                    }
                     id = _dataId++;
                 }
                 _dataMap[id] = value;