Skip to content

Commit

Permalink
[Tizen.Core] Fix memory leak (#6336)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hjhun authored and bshsqa committed Sep 25, 2024
1 parent 2a2cdf7 commit 118857b
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/Tizen.Core/Tizen.Core/ChannelObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Security.Cryptography;

namespace Tizen.Core
{
Expand All @@ -30,7 +31,7 @@ public class ChannelObject : IDisposable
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.
Expand Down Expand Up @@ -109,8 +110,19 @@ public object Data
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;
Expand Down

0 comments on commit 118857b

Please sign in to comment.