Skip to content

Commit 180ca33

Browse files
committed
fix(settings): fix issue with device list (settings) crashing when too many devices
The icon cache would evict icon used by the software. Fixes SOUNDSWITCH-1XX Fixes SOUNDSWITCH-1XB Fixes SOUNDSWITCH-206
1 parent eb8c7b8 commit 180ca33

File tree

3 files changed

+12
-20
lines changed

3 files changed

+12
-20
lines changed

SoundSwitch.Common/Framework/Audio/Icon/AudioDeviceIconExtractor.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
* GNU General Public License for more details.
1313
********************************************************************/
1414

15+
#nullable enable
1516
using System;
1617
using Microsoft.Extensions.Caching.Memory;
1718
using NAudio.CoreAudioApi;
1819
using Serilog;
19-
using SoundSwitch.Common.Framework.Icon;
2020
using SoundSwitch.Common.Properties;
2121

2222
namespace SoundSwitch.Common.Framework.Audio.Icon
@@ -28,7 +28,7 @@ public class AudioDeviceIconExtractor
2828

2929
private static readonly IMemoryCache IconCache = new MemoryCache(new MemoryCacheOptions
3030
{
31-
SizeLimit = 5120
31+
SizeLimit = 3200
3232
});
3333

3434
private static string GetKey(MMDevice audioDevice, bool largeIcon)
@@ -45,7 +45,7 @@ private static string GetKey(MMDevice audioDevice, bool largeIcon)
4545
/// <returns></returns>
4646
public static System.Drawing.Icon ExtractIconFromPath(string path, DataFlow dataFlow, bool largeIcon)
4747
{
48-
System.Drawing.Icon ExtractAssociatedIcon()
48+
System.Drawing.Icon? ExtractAssociatedIcon()
4949
{
5050
if (path.EndsWith(".ico"))
5151
{
@@ -55,20 +55,24 @@ System.Drawing.Icon ExtractAssociatedIcon()
5555
var iconInfo = path.Split(',');
5656
var dllPath = iconInfo[0];
5757
var iconIndex = int.Parse(iconInfo[1]);
58-
return IconExtractor.Extract(dllPath, iconIndex, largeIcon);
58+
return System.Drawing.Icon.ExtractIcon(dllPath, iconIndex, !largeIcon);
5959
}
6060

6161
var key = $"{path}-${largeIcon}";
6262

6363
if (IconCache.TryGetValue<System.Drawing.Icon>(key, out var icon))
64-
return icon;
64+
return icon!;
6565

6666
try
6767
{
6868
icon = ExtractAssociatedIcon();
69+
if (icon == null)
70+
{
71+
throw new ArgumentException("Can't find icon");
72+
}
6973
using var entry = IconCache.CreateEntry(key);
7074
entry.SetValue(icon)
71-
.SetSize(icon.Size.Height * icon.Size.Width)
75+
.SetSize(icon.Size.Height)
7276
.SetSlidingExpiration(TimeSpan.FromMinutes(30))
7377
.SetPriority(largeIcon ? CacheItemPriority.High : CacheItemPriority.Low)
7478
.RegisterPostEvictionCallback((o, value, reason, state) =>

SoundSwitch.Common/Framework/Icon/IconExtractor.cs

+1-13
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
//source : https://stackoverflow.com/questions/6872957/how-can-i-use-the-images-within-shell32-dll-in-my-c-sharp-project
1515

1616
using System;
17-
using System.Runtime.InteropServices;
1817
using System.Runtime.Serialization;
1918

2019
namespace SoundSwitch.Common.Framework.Icon
@@ -49,25 +48,14 @@ public static class IconExtractor
4948
/// <returns></returns>
5049
public static System.Drawing.Icon Extract(string file, int iconIndex, bool largeIcon)
5150
{
52-
IntPtr large;
53-
IntPtr small;
54-
NativeMethods.ExtractIconEx(file, iconIndex, out large, out small, 1);
5551
try
5652
{
57-
return System.Drawing.Icon.FromHandle(largeIcon ? large : small);
53+
return System.Drawing.Icon.ExtractIcon(file, iconIndex, !largeIcon);
5854
}
5955
catch(Exception e)
6056
{
6157
throw new IconExtractionException($"Can't extract icon from file: {file} / index:{iconIndex}", e);
6258
}
6359
}
64-
65-
private static class NativeMethods
66-
{
67-
[DllImport("Shell32.dll", EntryPoint = "ExtractIconExW", CharSet = CharSet.Unicode, ExactSpelling = true,
68-
CallingConvention = CallingConvention.StdCall)]
69-
public static extern int ExtractIconEx(string sFile, int iIndex, out IntPtr piLargeVersion,
70-
out IntPtr piSmallVersion, int amountIcons);
71-
}
7260
}
7361
}

SoundSwitch.Common/SoundSwitch.Common.csproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netstandard2.1</TargetFramework>
3+
<TargetFramework>net8.0-windows</TargetFramework>
44
<OutputType>Library</OutputType>
55
<Deterministic>false</Deterministic>
66
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>

0 commit comments

Comments
 (0)