View Issue Details

IDProjectCategoryView StatusLast Update
0022552LazarusLCLpublic2019-04-22 17:30
ReporterMartin Friebe Assigned ToZeljan Rikalo  
PrioritynormalSeverityminorReproducibilityalways
Status closedResolutionfixed 
Product Version1.1 (SVN) 
Summary0022552: ListView.Items.Delete causes excepion
DescriptionSee the code below. It throws an exception "index 1 out of bounds" on the 2nd delete.

The reason is FCacheIndex = 1 / lcl\include\listitems.inc line 280

procedure TListItems.ItemDestroying(const AItem: TListItem);
var
  idx: Integer;
begin
  // Don't use IndexOf, it updates the cache, which then will become invalid
  //DebugLn('TListItems.ItemDestroying ',dbgs(AItem));
  if (FCacheIndex <> -1) and (FCacheItem = AItem) then
    idx := FCacheIndex
  else
    idx := FItems.IndexOf(AItem);


This line sets FCacheIndex
  Item := ListView.Items[1];

Delete does not explicitly reset FCacheIndex, but obviously the cached item is no longer at index=1

If the listview has a handle, then it is reset here (lcl\include\customlistview.inc 265):
procedure TCustomListView.CNNotify(var AMessage: TLMNotify);
..
    LVN_DELETEITEM: begin
      Item := FListItems[nm^.iItem];

But clearly it is not save to rely on this.


This is also the cause for 0022541
Additional Informationprocedure TForm1.Button1Click(Sender: TObject);
var
  Item: TListItem;
  ListView: TListView;
begin
  ListView := TListView.Create(self);

  Item := ListView.Items.Add;
  Item := ListView.Items.Add;
  Item := ListView.Items[1];

  ListView.Items.Delete(0);
  ListView.Items.Delete(0);
end;
TagsNo tags attached.
Fixed in Revision38145
LazTarget-
Widgetset
Attached Files

Relationships

child of 0022541 resolvedMartin Friebe debugger fails after single stepping 

Activities

Martin Friebe

2012-08-02 21:41

manager   ~0061365

actually TListItems.ItemDestroying has

  if FCacheIndex = idx then
    FCacheIndex := -1;


But that does not help if FCacheIndex = 1, when delete(0)

So at the very least it must be

  if FCacheIndex >= idx then
    FCacheIndex := -1;

Maybe best to do it unconditionally

Zeljan Rikalo

2012-08-04 07:20

developer   ~0061407

I'll take a look at that part and try to fix it for RC2.

Zeljan Rikalo

2012-08-04 10:32

developer   ~0061411

Please test and close if ok.

Issue History

Date Modified Username Field Change
2012-08-02 21:38 Martin Friebe New Issue
2012-08-02 21:38 Martin Friebe LazTarget => -
2012-08-02 21:39 Martin Friebe Relationship added child of 0022541
2012-08-02 21:41 Martin Friebe Note Added: 0061365
2012-08-04 07:20 Zeljan Rikalo Note Added: 0061407
2012-08-04 07:20 Zeljan Rikalo Assigned To => Zeljan Rikalo
2012-08-04 07:20 Zeljan Rikalo Status new => assigned
2012-08-04 10:32 Zeljan Rikalo Fixed in Revision => 38145
2012-08-04 10:32 Zeljan Rikalo Status assigned => resolved
2012-08-04 10:32 Zeljan Rikalo Resolution open => fixed
2012-08-04 10:32 Zeljan Rikalo Note Added: 0061411
2019-04-22 17:30 Martin Friebe Status resolved => closed