Skip to content

Commit c1d08e2

Browse files
committed
fix: crashes when trying to close a repository with a action that is still running (#2289)
- It's not necessary to implement `IDisposable` interface for class that only contains managed resources - Do not clear fields of `Repository` and let system GC handle it Signed-off-by: leo <longshuang@msn.cn>
1 parent 3176bcc commit c1d08e2

11 files changed

Lines changed: 24 additions & 148 deletions

src/Models/Count.cs

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,4 @@
1-
using System;
2-
3-
namespace SourceGit.Models
1+
namespace SourceGit.Models
42
{
5-
public class Count : IDisposable
6-
{
7-
public int Value { get; set; } = 0;
8-
9-
public Count(int value)
10-
{
11-
Value = value;
12-
}
13-
14-
public void Dispose()
15-
{
16-
// Ignore
17-
}
18-
}
3+
public record Count(int Value);
194
}

src/Models/RepositoryUIStates.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,10 @@ public static RepositoryUIStates Load(string gitDir)
246246
return states;
247247
}
248248

249-
public void Unload(string lastCommitMessage)
249+
public void Unload()
250250
{
251251
try
252252
{
253-
LastCommitMessage = lastCommitMessage;
254253
using var stream = File.Create(_file);
255254
JsonSerializer.Serialize(stream, this, JsonCodeGen.Default.RepositoryUIStates);
256255
}

src/ViewModels/CommitDetail.cs

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public CommitDetailSharedData()
2525
}
2626
}
2727

28-
public partial class CommitDetail : ObservableObject, IDisposable
28+
public partial class CommitDetail : ObservableObject
2929
{
3030
public Repository Repository
3131
{
@@ -174,23 +174,6 @@ public CommitDetail(Repository repo, CommitDetailSharedData sharedData)
174174
WebLinks = Models.CommitLink.Get(repo.Remotes);
175175
}
176176

177-
public void Dispose()
178-
{
179-
_repo = null;
180-
_commit = null;
181-
_changes = null;
182-
_visibleChanges = null;
183-
_selectedChanges = null;
184-
_signInfo = null;
185-
_searchChangeFilter = null;
186-
_diffContext = null;
187-
_viewRevisionFileContent = null;
188-
_cancellationSource = null;
189-
_requestingRevisionFiles = false;
190-
_revisionFiles = null;
191-
_revisionFileSearchSuggestion = null;
192-
}
193-
194177
public void NavigateTo(string commitSHA)
195178
{
196179
_repo?.NavigateToCommit(commitSHA);

src/ViewModels/Histories.cs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace SourceGit.ViewModels
1212
{
13-
public class Histories : ObservableObject, IDisposable
13+
public class Histories : ObservableObject
1414
{
1515
public bool IsLoading
1616
{
@@ -89,7 +89,7 @@ public long NavigationId
8989
private set => SetProperty(ref _navigationId, value);
9090
}
9191

92-
public IDisposable DetailContext
92+
public object DetailContext
9393
{
9494
get => _detailContext;
9595
set => SetProperty(ref _detailContext, value);
@@ -131,16 +131,6 @@ public Histories(Repository repo)
131131
_commitDetailSharedData = new CommitDetailSharedData();
132132
}
133133

134-
public void Dispose()
135-
{
136-
Commits = [];
137-
_repo = null;
138-
_graph = null;
139-
_selectedCommit = null;
140-
_detailContext?.Dispose();
141-
_detailContext = null;
142-
}
143-
144134
public Models.BisectState UpdateBisectInfo()
145135
{
146136
var test = Path.Combine(_repo.GitDir, "BISECT_START");
@@ -454,7 +444,7 @@ public void CompareWithWorktree(Models.Commit commit)
454444
private Models.Commit _selectedCommit = null;
455445
private Models.Bisect _bisect = null;
456446
private long _navigationId = 0;
457-
private IDisposable _detailContext = null;
447+
private object _detailContext = null;
458448
private bool _ignoreSelectionChange = false;
459449

460450
private GridLength _leftArea = new GridLength(1, GridUnitType.Star);

src/ViewModels/Repository.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -483,10 +483,14 @@ public void Open()
483483

484484
public void Close()
485485
{
486-
SelectedView = null; // Do NOT modify. Used to remove exists widgets for GC.Collect
487-
Logs.Clear();
486+
SelectedView = new Models.Null();
488487

489-
_uiStates.Unload(_workingCopy.CommitMessage);
488+
var commitMessage = _workingCopy.CommitMessage;
489+
if (!string.IsNullOrEmpty(commitMessage) && _workingCopy.InProgressContext != null)
490+
File.WriteAllText(Path.Combine(GitDir, "MERGE_MSG"), commitMessage);
491+
492+
_uiStates.LastCommitMessage = commitMessage;
493+
_uiStates.Unload();
490494

491495
if (_cancellationRefreshBranches is { IsCancellationRequested: false })
492496
_cancellationRefreshBranches.Cancel();
@@ -499,35 +503,8 @@ public void Close()
499503
if (_cancellationRefreshStashes is { IsCancellationRequested: false })
500504
_cancellationRefreshStashes.Cancel();
501505

502-
_autoFetchTimer.Dispose();
503-
_autoFetchTimer = null;
504-
505-
_settings = null;
506-
_uiStates = null;
507-
_historyFilterMode = Models.FilterMode.None;
508-
509506
_watcher?.Dispose();
510-
_histories.Dispose();
511-
_workingCopy.Dispose();
512-
_stashesPage.Dispose();
513-
_searchCommitContext.Dispose();
514-
515-
_watcher = null;
516-
_histories = null;
517-
_workingCopy = null;
518-
_stashesPage = null;
519-
520-
_localChangesCount = 0;
521-
_stashesCount = 0;
522-
523-
_remotes.Clear();
524-
_branches.Clear();
525-
_localBranchTrees.Clear();
526-
_remoteBranchTrees.Clear();
527-
_tags.Clear();
528-
_visibleTags = null;
529-
_submodules.Clear();
530-
_visibleSubmodules = null;
507+
_autoFetchTimer.Dispose();
531508
}
532509

533510
public void SendNotification(string message, bool isError = false)

src/ViewModels/RevisionCompare.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SourceGit.ViewModels
1010
{
11-
public class RevisionCompare : ObservableObject, IDisposable
11+
public class RevisionCompare : ObservableObject
1212
{
1313
public bool IsLoading
1414
{
@@ -109,18 +109,6 @@ public RevisionCompare(Repository repo, Models.Commit startPoint, Models.Commit
109109
Refresh();
110110
}
111111

112-
public void Dispose()
113-
{
114-
_repo = null;
115-
_startPoint = null;
116-
_endPoint = null;
117-
_changes?.Clear();
118-
_visibleChanges?.Clear();
119-
_selectedChanges?.Clear();
120-
_searchFilter = null;
121-
_diffContext = null;
122-
}
123-
124112
public void OpenChangeWithExternalDiffTool(Models.Change change)
125113
{
126114
var opt = new Models.DiffOption(GetSHA(_startPoint), GetSHA(_endPoint), change);

src/ViewModels/SearchCommitContext.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace SourceGit.ViewModels
99
{
10-
public class SearchCommitContext : ObservableObject, IDisposable
10+
public class SearchCommitContext : ObservableObject
1111
{
1212
public int Method
1313
{
@@ -75,14 +75,6 @@ public SearchCommitContext(Repository repo)
7575
_repo = repo;
7676
}
7777

78-
public void Dispose()
79-
{
80-
_repo = null;
81-
_suggestions?.Clear();
82-
_results?.Clear();
83-
_worktreeFiles?.Clear();
84-
}
85-
8678
public void ClearFilter()
8779
{
8880
Filter = string.Empty;

src/ViewModels/StashesPage.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace SourceGit.ViewModels
1010
{
11-
public class StashesPage : ObservableObject, IDisposable
11+
public class StashesPage : ObservableObject
1212
{
1313
public List<Models.Stash> Stashes
1414
{
@@ -125,18 +125,6 @@ public StashesPage(Repository repo)
125125
_repo = repo;
126126
}
127127

128-
public void Dispose()
129-
{
130-
_stashes?.Clear();
131-
_changes?.Clear();
132-
_selectedChanges?.Clear();
133-
_untracked.Clear();
134-
135-
_repo = null;
136-
_selectedStash = null;
137-
_diffContext = null;
138-
}
139-
140128
public void ClearSearchFilter()
141129
{
142130
SearchFilter = string.Empty;

src/ViewModels/WorkingCopy.cs

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace SourceGit.ViewModels
88
{
9-
public class WorkingCopy : ObservableObject, IDisposable
9+
public class WorkingCopy : ObservableObject
1010
{
1111
public Repository Repository
1212
{
@@ -229,36 +229,6 @@ public WorkingCopy(Repository repo)
229229
_repo = repo;
230230
}
231231

232-
public void Dispose()
233-
{
234-
if (_inProgressContext != null && !string.IsNullOrEmpty(_commitMessage))
235-
File.WriteAllText(Path.Combine(_repo.GitDir, "MERGE_MSG"), _commitMessage);
236-
237-
_repo = null;
238-
_inProgressContext = null;
239-
240-
_selectedUnstaged.Clear();
241-
OnPropertyChanged(nameof(SelectedUnstaged));
242-
243-
_selectedStaged.Clear();
244-
OnPropertyChanged(nameof(SelectedStaged));
245-
246-
_visibleUnstaged.Clear();
247-
OnPropertyChanged(nameof(VisibleUnstaged));
248-
249-
_visibleStaged.Clear();
250-
OnPropertyChanged(nameof(VisibleStaged));
251-
252-
_unstaged.Clear();
253-
OnPropertyChanged(nameof(Unstaged));
254-
255-
_staged.Clear();
256-
OnPropertyChanged(nameof(Staged));
257-
258-
_detailContext = null;
259-
_commitMessage = string.Empty;
260-
}
261-
262232
public void SetData(List<Models.Change> changes)
263233
{
264234
if (!IsChanged(_cached, changes))

src/Views/LauncherPage.axaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
<!-- Body -->
3333
<Border Grid.Row="1">
34-
<ContentControl Content="{Binding Data}">
34+
<ContentControl Content="{Binding Data}">
3535
<ContentControl.DataTemplates>
3636
<DataTemplate DataType="vm:Welcome">
3737
<v:Welcome/>

0 commit comments

Comments
 (0)