Recent

Author Topic: TCollection.Delete, how come... question  (Read 266 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1079
    • http://www.cdbc.dk
TCollection.Delete, how come... question
« on: September 26, 2022, 01:07:53 pm »
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  [Select][+][-]
  1. procedure TCollection.Delete(Index: Integer);
  2. Var
  3.   Item : TCollectionItem;
  4. begin
  5.   Item:=TCollectionItem(FItems[Index]);
  6.   Notify(Item,cnDeleting); //<----------
  7.   Item.Free;                     //<----------
  8. end;
In it's private parts, we find TCollection.RemoveItem, which does the deleting/removing business:
Code: Pascal  [Select][+][-]
  1. procedure TCollection.RemoveItem(Item: TCollectionItem);
  2.  
  3. Var
  4.   I : Integer;
  5.  
  6. begin
  7.   Notify(Item,cnExtracting);
  8.   I:=FItems.IndexOfItem(Item,fromEnd);
  9.   If (I<>-1) then
  10.     FItems.Delete(I);       //<-----------
  11.   Item.FCollection:=Nil;  //<-----------
  12.   Changed;
  13. 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
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

PascalDragon

  • Hero Member
  • *****
  • Posts: 5478
  • Compiler Developer
Re: TCollection.Delete, how come... question
« Reply #1 on: September 26, 2022, 01:14:01 pm »
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

  • Hero Member
  • *****
  • Posts: 1079
    • http://www.cdbc.dk
Re: TCollection.Delete, how come... question
« Reply #2 on: September 26, 2022, 01:36:55 pm »
Hi
@PascalDragon:
Thank you, the destructor part eluded me  %)
Then all is fine and dandy  :D
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018