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
This commit is contained in:
Markus Ewald 2010-04-16 10:26:06 +00:00
parent f0289632f6
commit a0a0b73a13

View File

@ -311,25 +311,20 @@ namespace Nuclex.Support.Collections {
/// Removes the items that have been garbage collected from the collection /// Removes the items that have been garbage collected from the collection
/// </summary> /// </summary>
public void RemoveDeadItems() { public void RemoveDeadItems() {
int eliminatedItemCount = 0; int newCount = 0;
// Eliminate all items that have been garbage collected by shifting // Eliminate all items that have been garbage collected by shifting
for(int index = 0; index + eliminatedItemCount < this.items.Count; ++index) { for(int index = 0; index < this.items.Count; ++index) {
if(!this.items[index].IsAlive) { if(this.items[index].IsAlive) {
++eliminatedItemCount; this.items[newCount] = this.items[index];
} else { ++newCount;
this.items[index] = this.items[index + eliminatedItemCount];
++index;
} }
} }
// If any garbage collected items were found, resize the collection so // If any garbage collected items were found, resize the collection so
// the space that became empty in the previous shifting process will be freed // the space that became empty in the previous shifting process will be freed
if(eliminatedItemCount > 0) { while(this.items.Count > newCount) {
int endIndex = this.items.Count - eliminatedItemCount; this.items.RemoveAt(this.items.Count - 1);
for(int index = this.items.Count - 1; index >= endIndex; --index) {
this.items.RemoveAt(index);
}
} }
} }