Forum > General

TCollection.Delete, how come... question

(1/1)

cdbc:
Hi
How come that TCollection.Delete does not call TCollection.RemoveItem?!?
It only frees the item and notifies observers. The slot remains in the collection.
Excerpt from collect.inc:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TCollection.Delete(Index: Integer);Var  Item : TCollectionItem;begin  Item:=TCollectionItem(FItems[Index]);  Notify(Item,cnDeleting); //<----------  Item.Free;                     //<----------end;In it's private parts, we find TCollection.RemoveItem, which does the deleting/removing business:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TCollection.RemoveItem(Item: TCollectionItem); Var  I : Integer; begin  Notify(Item,cnExtracting);  I:=FItems.IndexOfItem(Item,fromEnd);  If (I<>-1) then    FItems.Delete(I);       //<-----------  Item.FCollection:=Nil;  //<-----------  Changed;end; It doesn't get called or somthing similar done, to remove the item.
What happens if one deletes an item and then afterwards traverses the collection?!? Wouldn't that be an accident waiting to happen?!? Bug?!?
Regards Benny

PascalDragon:
The destructor of TCollectionItem ensures that the item removes itself from the collection. RemoveItem only removes the item from the collection, but does not free the TCollectionItem itself (what Delete does).

cdbc:
Hi
@PascalDragon:
Thank you, the destructor part eluded me  %)
Then all is fine and dandy  :D
Regards Benny

Navigation

[0] Message Index

Go to full version