From a0a0b73a13aa04732011e5cda890172891a2a017 Mon Sep 17 00:00:00 2001 From: Markus Ewald Date: Fri, 16 Apr 2010 10:26:06 +0000 Subject: [PATCH] Fixed a nasty bug that would corrupt the WeakCollection upon calling RemoveDeadItems() git-svn-id: file:///srv/devel/repo-conversion/nusu@194 d2e56fa2-650e-0410-a79f-9358c0239efd --- Source/Collections/WeakCollection.cs | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Source/Collections/WeakCollection.cs b/Source/Collections/WeakCollection.cs index fe02f52..809efc7 100644 --- a/Source/Collections/WeakCollection.cs +++ b/Source/Collections/WeakCollection.cs @@ -311,25 +311,20 @@ namespace Nuclex.Support.Collections { /// Removes the items that have been garbage collected from the collection /// public void RemoveDeadItems() { - int eliminatedItemCount = 0; + int newCount = 0; // Eliminate all items that have been garbage collected by shifting - for(int index = 0; index + eliminatedItemCount < this.items.Count; ++index) { - if(!this.items[index].IsAlive) { - ++eliminatedItemCount; - } else { - this.items[index] = this.items[index + eliminatedItemCount]; - ++index; + for(int index = 0; index < this.items.Count; ++index) { + if(this.items[index].IsAlive) { + this.items[newCount] = this.items[index]; + ++newCount; } } // If any garbage collected items were found, resize the collection so // the space that became empty in the previous shifting process will be freed - if(eliminatedItemCount > 0) { - int endIndex = this.items.Count - eliminatedItemCount; - for(int index = this.items.Count - 1; index >= endIndex; --index) { - this.items.RemoveAt(index); - } + while(this.items.Count > newCount) { + this.items.RemoveAt(this.items.Count - 1); } }