[MedaiTool]Fixed possible memory leak
authorcoderhyme <jhyo.kim@samsung.com>
Thu, 23 Mar 2017 07:48:38 +0000 (16:48 +0900)
committercoderhyme <jhyo.kim@samsung.com>
Wed, 5 Apr 2017 09:44:53 +0000 (18:44 +0900)
[MediaCodec]Changed event implementations

Change-Id: I7c600524e1fb0383072cd339677361d963f458b5
Signed-off-by: coderhyme <jhyo.kim@samsung.com>
src/Tizen.Multimedia/MediaCodec/MediaCodec.cs
src/Tizen.Multimedia/MediaTool/MediaPacket.cs

index 76b1076..be828ae 100644 (file)
@@ -45,6 +45,9 @@ namespace Tizen.Multimedia.MediaCodec
             }
 
             MultimediaDebug.AssertNoError(ret);
+
+            RegisterInputProcessed();
+            RegisterErrorOccurred();
         }
 
         #region IDisposable-support
@@ -337,7 +340,7 @@ namespace Tizen.Multimedia.MediaCodec
         }
 
         /// <summary>
-        /// Add the packet to the internal queue of the codec.
+        /// Adds the packet to the internal queue of the codec.
         /// </summary>
         /// <param name="packet">The packet to be encoded or decoded.</param>
         /// <exception cref="ArgumentNullException">packet is null.</exception>
@@ -352,7 +355,7 @@ namespace Tizen.Multimedia.MediaCodec
                 throw new ArgumentNullException(nameof(packet));
             }
 
-            MediaPacket.Lock packetLock = new MediaPacket.Lock(packet);
+            MediaPacket.Lock packetLock = MediaPacket.Lock.Get(packet);
 
             int ret = Interop.MediaCodec.Process(_handle, packetLock.GetHandle(), 0);
 
@@ -365,7 +368,7 @@ namespace Tizen.Multimedia.MediaCodec
         }
 
         /// <summary>
-        /// Flush both input and output buffers.
+        /// Flushes both input and output buffers.
         /// </summary>
         public void FlushBuffers()
         {
@@ -508,37 +511,13 @@ namespace Tizen.Multimedia.MediaCodec
         #endregion
 
         #region InputProcessed event
-        private EventHandler<InputProcessedEventArgs> _inputProcessed;
         private Interop.MediaCodec.InputBufferUsedCallback _inputBufferUsedCb;
 
         /// <summary>
         /// Occurs when an input packet is processed.
         /// </summary>
         /// <see cref="ProcessInput(MediaPacket)"/>
-        public event EventHandler<InputProcessedEventArgs> InputProcessed
-        {
-            add
-            {
-                ValidateNotDisposed();
-
-                if (_inputProcessed == null)
-                {
-                    RegisterInputProcessed();
-                }
-                _inputProcessed += value;
-
-            }
-            remove
-            {
-                ValidateNotDisposed();
-
-                _inputProcessed -= value;
-                if (_inputProcessed == null)
-                {
-                    UnregisterInputProcessed();
-                }
-            }
-        }
+        public event EventHandler<InputProcessedEventArgs> InputProcessed;
 
         private void RegisterInputProcessed()
         {
@@ -550,11 +529,13 @@ namespace Tizen.Multimedia.MediaCodec
                 using (MediaPacket.Lock packetLock =
                     MediaPacket.Lock.FromHandle(lockedPacketHandle))
                 {
+                    Debug.Assert(packetLock != null);
+
                     packet = packetLock.MediaPacket;
                 }
                 Debug.Assert(packet != null);
 
-                _inputProcessed?.Invoke(this, new InputProcessedEventArgs(packet));
+                InputProcessed?.Invoke(this, new InputProcessedEventArgs(packet));
             };
 
             int ret = Interop.MediaCodec.SetInputBufferUsedCb(_handle,
@@ -572,37 +553,12 @@ namespace Tizen.Multimedia.MediaCodec
         #endregion
 
         #region ErrorOccurred event
-        private EventHandler<MediaCodecErrorOccurredEventArgs> _errorOccurred;
         private Interop.MediaCodec.ErrorCallback _errorCb;
 
-        // TODO replace
         /// <summary>
         /// Occurs whenever an error is produced in the codec.
         /// </summary>
-        public event EventHandler<MediaCodecErrorOccurredEventArgs> ErrorOccurred
-        {
-            add
-            {
-                ValidateNotDisposed();
-
-                if (_errorOccurred == null)
-                {
-                    RegisterErrorOccurred();
-                }
-                _errorOccurred += value;
-
-            }
-            remove
-            {
-                ValidateNotDisposed();
-
-                _errorOccurred -= value;
-                if (_errorOccurred == null)
-                {
-                    UnregisterErrorOccurred();
-                }
-            }
-        }
+        public event EventHandler<MediaCodecErrorOccurredEventArgs> ErrorOccurred;
 
         private void RegisterErrorOccurred()
         {
@@ -611,7 +567,7 @@ namespace Tizen.Multimedia.MediaCodec
                 MediaCodecError error = (Enum.IsDefined(typeof(MediaCodecError), errorCode)) ?
                     (MediaCodecError)errorCode : MediaCodecError.InternalError;
 
-                _errorOccurred?.Invoke(this, new MediaCodecErrorOccurredEventArgs(error));
+                ErrorOccurred?.Invoke(this, new MediaCodecErrorOccurredEventArgs(error));
             };
             int ret = Interop.MediaCodec.SetErrorCb(_handle, _errorCb, IntPtr.Zero);
 
index c31f541..8705e40 100644 (file)
@@ -603,8 +603,28 @@ namespace Tizen.Multimedia
         {
             private readonly MediaPacket _packet;
             private readonly GCHandle _gcHandle;
+            private int _lockCount;
 
-            internal Lock(MediaPacket packet)
+            internal static Lock Get(MediaPacket packet)
+            {
+                Debug.Assert(packet != null);
+
+                lock (packet)
+                {
+                    Lock lck = FromHandle(packet._handle);
+
+                    if (lck == null)
+                    {
+                        lck = new Lock(packet);
+                    }
+
+                    lck._lockCount++;
+
+                    return lck;
+                }
+            }
+
+            private Lock(MediaPacket packet)
             {
                 Debug.Assert(packet != null, "The packet is null!");
 
@@ -625,7 +645,10 @@ namespace Tizen.Multimedia
 
                 IntPtr extra = GetExtra(handle);
 
-                Debug.Assert(extra != IntPtr.Zero, "Extra of packet must not be null.");
+                if (extra == IntPtr.Zero)
+                {
+                    return null;
+                }
 
                 return (Lock)GCHandle.FromIntPtr(extra).Target;
             }
@@ -667,16 +690,24 @@ namespace Tizen.Multimedia
             {
                 if (!_isDisposed)
                 {
-                    // TODO rollback after the corresponding native api is fixed.
-                    //SetExtra(IntPtr.Zero);
-
-                    if (_gcHandle.IsAllocated)
+                    lock (_packet)
                     {
-                        _gcHandle.Free();
-                    }
+                        _lockCount--;
+
+                        if (_lockCount == 0)
+                        {
+                            // TODO rollback after the corresponding native api is fixed.
+                            //SetExtra(IntPtr.Zero);
 
-                    //We can assure that at this point '_packet' is always locked by this lock.
-                    _packet._lock.SetUnlock();
+                            if (_gcHandle.IsAllocated)
+                            {
+                                _gcHandle.Free();
+                            }
+
+                            //We can assure that at this point '_packet' is always locked by this lock.
+                            _packet._lock.SetUnlock();
+                        }
+                    }
 
                     _isDisposed = true;
                 }