Skip to content

Commit

Permalink
- Fixed some crashes in previous commits
Browse files Browse the repository at this point in the history
! Addessed memory leak (#173)
  • Loading branch information
wmjordan committed Oct 27, 2021
1 parent 6f35b99 commit fdadc0c
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 13 deletions.
7 changes: 6 additions & 1 deletion Codist/Controls/ExternalAdornment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,12 @@ public void FocusOnTextView() {
public void ClearUnpinnedChildren() {
for (int i = Children.Count - 1; i >= 0; i--) {
if (Children[i] is SymbolList l && l.IsPinned == false) {
Children.RemoveAndDisposeAt(i);
if (l.Owner == null) {
Children.RemoveAndDisposeAt(i);
}
else {
Children.RemoveAt(i);
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions Codist/Controls/VirtualList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public double ItemsControlMaxHeight {
}
public ListCollectionView FilteredItems { get; set; }
public FrameworkElement Container { get; set; }
public FrameworkElement Owner { get; set; }
public bool NeedsRefresh { get; set; }
public bool EnableVirtualMode {
get => ScrollViewer.GetCanContentScroll(this);
Expand Down Expand Up @@ -125,6 +126,7 @@ protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo) {
public virtual void Dispose() {
Container = null;
FilteredItems = null;
Owner = null;
DataContext = null;
}
}
Expand Down
2 changes: 1 addition & 1 deletion Codist/Helpers/WpfHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ public static bool FocusLast<TItem>(this ItemCollection items)
return false;
}
/// <summary>
/// Calls <see cref="IDisposable.Dispose"/> on each item in <paramref name="control"/> and empties the collection.
/// Calls <see cref="IDisposable.Dispose"/> on each item in <paramref name="control"/> and empties the <see cref="ItemsControl"/>.
/// </summary>
public static void DisposeCollection(this ItemsControl control) {
foreach (var item in control.Items) {
Expand Down
13 changes: 5 additions & 8 deletions Codist/NaviBar/CSharpBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,6 @@ void View_Closed(object sender, EventArgs e) {
_ActiveItem = null;
_GlobalNamespaceItem = null;
_RootItem = null;
foreach (var item in Items) {
if (item is IDisposable d) {
d.Dispose();
}
}
Items.Clear();
}

void DisposeSymbolList(SymbolList l) {
Expand Down Expand Up @@ -531,7 +525,8 @@ sealed class RootItem : BarItem, IContextMenuHost
}
},
Footer = _Note = new TextBlock { Margin = WpfHelper.MenuItemMargin }
.ReferenceProperty(TextBlock.ForegroundProperty, EnvironmentColors.SystemGrayTextBrushKey)
.ReferenceProperty(TextBlock.ForegroundProperty, EnvironmentColors.SystemGrayTextBrushKey),
Owner = this
};
Codist.Controls.DragDropHelper.SetScrollOnDragDrop(_Menu, true);
Bar.SetupSymbolListMenu(_Menu);
Expand Down Expand Up @@ -942,7 +937,8 @@ async Task CreateMenuForNamespaceNodeAsync(CancellationToken cancellationToken)
Container = Bar.ListContainer,
ContainerType = SymbolListType.TypeList,
ExtIconProvider = ExtIconProvider.Default.GetExtIcons,
EnableVirtualMode = true
EnableVirtualMode = true,
Owner = this
};
Codist.Controls.DragDropHelper.SetScrollOnDragDrop(_Menu, true);
if (_FilterBox != null) {
Expand Down Expand Up @@ -1128,6 +1124,7 @@ async Task CreateMenuForTypeSymbolNodeAsync(CancellationToken cancellationToken)
Container = Bar.ListContainer,
ContainerType = SymbolListType.NodeList,
ExtIconProvider = s => GetExtIcons(s.SyntaxNode),
Owner = this
};
Codist.Controls.DragDropHelper.SetScrollOnDragDrop(_Menu, true);
_Menu.Header = new WrapPanel {
Expand Down
8 changes: 6 additions & 2 deletions Codist/NaviBar/MarkdownBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ void HideMenu() {
if (_TitleList != null) {
ListContainer.Children.Remove(_TitleList);
_TitleList.SelectedItem = null;
_TitleList.Dispose();
_TitleList = null;
_ActiveItem.IsHighlighted = false;
}
Expand Down Expand Up @@ -133,6 +134,7 @@ public override void ShowRootItemMenu(int parameter) {
if (_TitleList != menu) {
ListContainer.Children.Remove(_TitleList);
ListContainer.Children.Add(menu);
_TitleList.Dispose();
_TitleList = menu;
}
if (menu != null) {
Expand Down Expand Up @@ -183,8 +185,10 @@ void View_Closed(object sender, EventArgs e) {
view.Closed -= View_Closed;
view.Properties.RemoveProperty(typeof(TaggerResult));
_TextSearch = null;
_TitleList.Dispose();
_TitleList = null;
if (_TitleList != null) {
_TitleList.Dispose();
_TitleList = null;
}
}

//void AddItem(int imageId, RoutedEventHandler clickHandler) {
Expand Down
2 changes: 2 additions & 0 deletions Codist/NaviBar/NaviBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ void View_Closed(object sender, EventArgs e) {
c.Content = null;
}
ListContainer = null;
DataContext = null;
this.DisposeCollection();
_View.Properties.RemoveProperty(nameof(NaviBar));
_View.Properties.RemoveProperty(typeof(ExternalAdornment));
_View = null;
Expand Down
1 change: 1 addition & 0 deletions Codist/SmartBars/SmartBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ public void Dispose() {
ContextMenu.PlacementTarget = null;
ContextMenu = null;
}
DataContext = null;
_Bar = null;
}
}
Expand Down
5 changes: 4 additions & 1 deletion Codist/Taggers/CSharpTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,10 @@ public void Dispose() {

void ReleaseResources() {
if (_Buffer != null) {
_TaskBreaker?.Dispose();
if (_TaskBreaker != null) {
_TaskBreaker.Dispose();
_TaskBreaker = null;
}
var last = _LastFinishedTask;
if (last != null) {
_LastFinishedTask = null;
Expand Down

0 comments on commit fdadc0c

Please sign in to comment.