Skip to content

Commit d757745

Browse files
authored
Revert "Code Quality: Pin WinRT server object to prevent object reference being invalid" (#15346)
1 parent 49a1664 commit d757745

File tree

5 files changed

+11
-85
lines changed

5 files changed

+11
-85
lines changed

src/Files.App.Server/AppInstanceMonitor.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
// Copyright (c) 2024 Files Community
22
// Licensed under the MIT License. See the LICENSE.
33

4-
using System.Collections.Concurrent;
54
using System.Diagnostics;
65

76
namespace Files.App.Server;
87

98
public sealed class AppInstanceMonitor
109
{
1110
private static int processCount = 0;
12-
internal static ConcurrentDictionary<int, ConcurrentBag<IDisposable>> AppInstanceResources = new();
1311

1412
public static void StartMonitor(int processId)
1513
{
@@ -21,18 +19,10 @@ public static void StartMonitor(int processId)
2119

2220
private static void Process_Exited(object? sender, EventArgs e)
2321
{
24-
if (sender is Process { Id: var processId } process)
22+
if (sender is Process process)
2523
{
2624
process.Dispose();
2725

28-
if (AppInstanceResources.TryRemove(processId, out var instances))
29-
{
30-
foreach (var instance in instances)
31-
{
32-
instance.Dispose();
33-
}
34-
}
35-
3626
if (Interlocked.Decrement(ref processCount) == 0)
3727
{
3828
Program.ExitSignalEvent.Set();

src/Files.App.Server/Database/FileTagsDatabase.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using LiteDB;
77
using Microsoft.Win32;
88
using System.Runtime.CompilerServices;
9-
using System.Runtime.InteropServices;
109
using System.Runtime.InteropServices.WindowsRuntime;
1110
using System.Text;
1211
using Windows.ApplicationModel;
@@ -18,16 +17,13 @@
1817

1918
namespace Files.App.Server.Database
2019
{
21-
public sealed class FileTagsDatabase : IDisposable
20+
public sealed class FileTagsDatabase
2221
{
2322
private readonly static string FileTagsKey = @$"Software\Files Community\{Package.Current.Id.FullName}\v1\FileTags";
2423

2524
private readonly static string FileTagsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "filetags.db");
2625
private const string FileTagsCollectionName = "taggedfiles";
2726

28-
private readonly GCHandle _handle;
29-
private bool _disposed = false;
30-
3127
static FileTagsDatabase()
3228
{
3329
if (File.Exists(FileTagsDbPath))
@@ -48,25 +44,6 @@ static FileTagsDatabase()
4844
}
4945
}
5046

51-
public FileTagsDatabase()
52-
{
53-
throw new NotSupportedException($"Instantiating {nameof(FileTagsDatabase)} by non-parameterized constructor is not supported.");
54-
}
55-
56-
public FileTagsDatabase(int processId)
57-
{
58-
_handle = GCHandle.Alloc(this, GCHandleType.Pinned);
59-
60-
if (AppInstanceMonitor.AppInstanceResources.TryGetValue(processId, out var instances))
61-
{
62-
instances.Add(this);
63-
}
64-
else
65-
{
66-
AppInstanceMonitor.AppInstanceResources[processId] = [this];
67-
}
68-
}
69-
7047
private static void UpdateDb(LiteDatabase database)
7148
{
7249
if (database.UserVersion == 0)
@@ -302,14 +279,5 @@ private void IterateKeys(List<TaggedFile> list, string path, int depth)
302279
IterateKeys(list, CombineKeys(path, subKey), depth + 1);
303280
}
304281
}
305-
306-
public void Dispose()
307-
{
308-
if (!_disposed)
309-
{
310-
_disposed = true;
311-
_handle.Free();
312-
}
313-
}
314282
}
315283
}

src/Files.App.Server/Database/LayoutPreferencesDatabase.cs

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
using LiteDB;
66
using Microsoft.Win32;
77
using System.Runtime.CompilerServices;
8-
using System.Runtime.InteropServices;
98
using Windows.ApplicationModel;
109
using Windows.Storage;
1110
using static Files.App.Server.Data.LayoutPreferencesRegistry;
@@ -14,16 +13,13 @@
1413

1514
namespace Files.App.Server.Database
1615
{
17-
public sealed class LayoutPreferencesDatabase : IDisposable
16+
public sealed class LayoutPreferencesDatabase
1817
{
1918
private readonly static string LayoutSettingsKey = @$"Software\Files Community\{Package.Current.Id.FullName}\v1\LayoutPreferences";
2019

2120
private readonly static string LayoutSettingsDbPath = Path.Combine(ApplicationData.Current.LocalFolder.Path, "user_settings.db");
2221
private const string LayoutSettingsCollectionName = "layoutprefs";
2322

24-
private readonly GCHandle _handle;
25-
private bool _disposed = false;
26-
2723
static LayoutPreferencesDatabase()
2824
{
2925
if (File.Exists(LayoutSettingsDbPath))
@@ -41,25 +37,6 @@ static LayoutPreferencesDatabase()
4137
}
4238
}
4339

44-
public LayoutPreferencesDatabase()
45-
{
46-
throw new NotSupportedException($"Instantiating {nameof(LayoutPreferencesDatabase)} by non-parameterized constructor is not supported.");
47-
}
48-
49-
public LayoutPreferencesDatabase(int processId)
50-
{
51-
_handle = GCHandle.Alloc(this, GCHandleType.Pinned);
52-
53-
if (AppInstanceMonitor.AppInstanceResources.TryGetValue(processId, out var instances))
54-
{
55-
instances.Add(this);
56-
}
57-
else
58-
{
59-
AppInstanceMonitor.AppInstanceResources[processId] = [this];
60-
}
61-
}
62-
6340
public LayoutPreferencesItem? GetPreferences(string filePath, ulong? frn)
6441
{
6542
return FindPreferences(filePath, frn)?.LayoutPreferencesManager;
@@ -220,14 +197,5 @@ private void IterateKeys(List<LayoutPreferences> list, string path, int depth)
220197

221198
return null;
222199
}
223-
224-
public void Dispose()
225-
{
226-
if (!_disposed)
227-
{
228-
_disposed = true;
229-
_handle.Free();
230-
}
231-
}
232200
}
233201
}

src/Files.App/Helpers/Layout/LayoutPreferencesDatabaseManager.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Files.App.Helpers
1111
public class LayoutPreferencesDatabaseManager
1212
{
1313
// Fields
14-
private readonly Server.Database.LayoutPreferencesDatabase _database = new(Environment.ProcessId);
14+
private static readonly Lazy<Server.Database.LayoutPreferencesDatabase> dbInstance = new(() => new());
1515

1616
private DetailsLayoutColumnItem FromDatabaseEntity(Server.Data.ColumnPreferencesItem entry)
1717
{
@@ -116,27 +116,27 @@ private Server.Data.ColumnPreferencesItem ToDatabaseEntity(DetailsLayoutColumnIt
116116
// Methods
117117
public LayoutPreferencesItem? GetPreferences(string filePath, ulong? frn = null)
118118
{
119-
return FromDatabaseEntity(_database.GetPreferences(filePath, frn));
119+
return FromDatabaseEntity(dbInstance.Value.GetPreferences(filePath, frn));
120120
}
121121

122122
public void SetPreferences(string filePath, ulong? frn, LayoutPreferencesItem? preferencesItem)
123123
{
124-
_database.SetPreferences(filePath, frn, ToDatabaseEntity(preferencesItem));
124+
dbInstance.Value.SetPreferences(filePath, frn, ToDatabaseEntity(preferencesItem));
125125
}
126126

127127
public void ResetAll()
128128
{
129-
_database.ResetAll();
129+
dbInstance.Value.ResetAll();
130130
}
131131

132132
public void Import(string json)
133133
{
134-
_database.Import(json);
134+
dbInstance.Value.Import(json);
135135
}
136136

137137
public string Export()
138138
{
139-
return _database.Export();
139+
return dbInstance.Value.Export();
140140
}
141141
}
142142
}

src/Files.App/Utils/FileTags/FileTagsHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ namespace Files.App.Utils.FileTags
1212
{
1313
public static class FileTagsHelper
1414
{
15-
private static readonly Server.Database.FileTagsDatabase dbInstance = new(Environment.ProcessId);
15+
private static readonly Lazy<Server.Database.FileTagsDatabase> dbInstance = new(() => new());
1616

17-
public static Server.Database.FileTagsDatabase GetDbInstance() => dbInstance;
17+
public static Server.Database.FileTagsDatabase GetDbInstance() => dbInstance.Value;
1818

1919
public static string[] ReadFileTag(string filePath)
2020
{

0 commit comments

Comments
 (0)