From 5ab5b0e709c3667ca85a6727620ff26f074e4ce5 Mon Sep 17 00:00:00 2001
From: gdkchan <gab.dark.100@gmail.com>
Date: Wed, 10 Mar 2021 17:36:38 -0300
Subject: [PATCH] Close ILibraryAppletAccessor handles on disposal (#2094)

---
 .../ILibraryAppletAccessor.cs                 | 25 ++++++++++++++++++-
 1 file changed, 24 insertions(+), 1 deletion(-)

diff --git a/Ryujinx.HLE/HOS/Services/Am/AppletAE/AllSystemAppletProxiesService/LibraryAppletCreator/ILibraryAppletAccessor.cs b/Ryujinx.HLE/HOS/Services/Am/AppletAE/AllSystemAppletProxiesService/LibraryAppletCreator/ILibraryAppletAccessor.cs
index 5f1e4c26f2..67e6d2ae2d 100644
--- a/Ryujinx.HLE/HOS/Services/Am/AppletAE/AllSystemAppletProxiesService/LibraryAppletCreator/ILibraryAppletAccessor.cs
+++ b/Ryujinx.HLE/HOS/Services/Am/AppletAE/AllSystemAppletProxiesService/LibraryAppletCreator/ILibraryAppletAccessor.cs
@@ -1,14 +1,17 @@
 using Ryujinx.Common.Logging;
 using Ryujinx.HLE.HOS.Applets;
 using Ryujinx.HLE.HOS.Ipc;
+using Ryujinx.HLE.HOS.Kernel;
 using Ryujinx.HLE.HOS.Kernel.Common;
 using Ryujinx.HLE.HOS.Kernel.Threading;
 using System;
 
 namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.LibraryAppletCreator
 {
-    class ILibraryAppletAccessor : IpcService
+    class ILibraryAppletAccessor : IpcService, IDisposable
     {
+        private KernelContext _kernelContext;
+
         private IApplet _applet;
 
         private AppletSession _normalSession;
@@ -24,6 +27,8 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.Lib
 
         public ILibraryAppletAccessor(AppletId appletId, Horizon system)
         {
+            _kernelContext = system.KernelContext;
+
             _stateChangedEvent       = new KEvent(system.KernelContext);
             _normalOutDataEvent      = new KEvent(system.KernelContext);
             _interactiveOutDataEvent = new KEvent(system.KernelContext);
@@ -223,5 +228,23 @@ namespace Ryujinx.HLE.HOS.Services.Am.AppletAE.AllSystemAppletProxiesService.Lib
 
             return ResultCode.Success;
         }
+
+        public void Dispose()
+        {
+            if (_stateChangedEventHandle != 0)
+            {
+                _kernelContext.Syscall.CloseHandle(_stateChangedEventHandle);
+            }
+
+            if (_normalOutDataEventHandle != 0)
+            {
+                _kernelContext.Syscall.CloseHandle(_normalOutDataEventHandle);
+            }
+
+            if (_interactiveOutDataEventHandle != 0)
+            {
+                _kernelContext.Syscall.CloseHandle(_interactiveOutDataEventHandle);
+            }
+        }
     }
 }