[SOLVED] TObjectList .delete() is not deleting lazarus / 21-General ---------------------------------------------------------------------- petevick · 2022-12-05 15:56 UndoList is the TObjectList component. I have UndoList.OwnsObjects set to True. All variables are declared. I don't get any errors, and Ctrl1.ButtonColor is getting the right color as it moves through the list, but UndoList.Delete(n) is not deleting and reducing the UndoList.Count by one at each loop. It has to be something to do with it being in a loop, but what?. What the hell am I missing ??  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     for n:= idx-1 downto idx - 48 do    begin     if FindComponent(UndoObj(UndoList[n]).Cntrl) is TColorButton     then begin     Ctrl1:=self.FindComponent(UndoObj(UndoList[n]).Cntrl) as TColorButton;     str2:= UndoObj(UndoList[n]).CntrlVal;     Ctrl1.ButtonColor:=StringToColor(str2);     if UndoList.Count > 0 then UndoList.Delete(n);     end;    end; ---------------------------------------------------------------------- Handoko · 2022-12-05 16:14 I believe the bug is not in the code you showed. Can you provide a simple demo that can show the issue? I mean a whole source code that we can open, run and test, not just some simple lines. ---------------------------------------------------------------------- MarkMLl · 2022-12-05 16:19 If anything, I'm uncomfortable with the way that n is a fixed range but then the test prior to deletion is .count > 0. What is the behaviour of Delete(n) if that element doesn't exist? I feel that's a general point: things that are in some way coupled should be coupled explicitly in the code, rather than assumptions being made. Put in a temporary variable containing the .count value, and monitor that using the debugger as the loop progresses. MarkMLl ---------------------------------------------------------------------- petevick · 2022-12-05 16:25 Quote from: MarkMLl on December 05, 2022, 05:19:53 pm...Put in a temporary variable containing the .count value, and monitor that using the debugger as the loop progresses. MarkMLl I should have mentioned that I've already done that, the .count value does not change, it remains at the value it was prior to entering the loop and throughout the loop. ---------------------------------------------------------------------- MarkMLl · 2022-12-05 16:36 Quote from: petevick on December 05, 2022, 05:25:14 pmI should have mentioned that I've already done that, the .count value does not change, it remains at the value it was prior to entering the loop and throughout the loop. Yes. But again: does .Count indicate that [n] actually exists? Are you sure that what you're trying to delete really is deletable? MarkMLl ---------------------------------------------------------------------- petevick · 2022-12-05 17:15 Quote from: MarkMLl on December 05, 2022, 05:36:44 pm Yes. But again: does .Count indicate that [n] actually exists? Are you sure that what you're trying to delete really is deletable? MarkMLl Sorry I misunderstood. Yes, n exists, it counts down as expected. As to whether the object is deletable is another question. I can't help feeling that UndoList.Delete(n) is still referencing the object so it can't release it, that said, I added Ctrl1 := Nil; just before the delete and it made no difference. ---------------------------------------------------------------------- KodeZwerg · 2022-12-05 17:24 Add a small project that demonstrate your usage to find out where it fail. That little snippet you showing it covers what you try to do but let too many questions unresolved. ---------------------------------------------------------------------- petevick · 2022-12-05 18:09 Quote from: KodeZwerg on December 05, 2022, 06:24:44 pmAdd a small project that demonstrate your usage to find out where it fail. That little snippet you showing it covers what you try to do but let too many questions unresolved. I'm sorry but I've been told this is as much of the code as I can publish, in any form. ---------------------------------------------------------------------- KodeZwerg · 2022-12-05 18:15 Quote from: petevick on December 05, 2022, 07:09:32 pmQuote from: KodeZwerg on December 05, 2022, 06:24:44 pmAdd a small project that demonstrate your usage to find out where it fail. That little snippet you showing it covers what you try to do but let too many questions unresolved. I'm sorry but I've been told this is as much of the code as I can publish, in any form. No need to be sorry and I hope you manage to find out where its doing something wrong ---------------------------------------------------------------------- petevick · 2022-12-05 18:24 Quote from: KodeZwerg on December 05, 2022, 07:15:51 pmNo need to be sorry and I hope you manage to find out where its doing something wrong Well I have found out that line 7 is causing the delete not to work........ Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i    Ctrl1.ButtonColor:=StringToColor(str2);just need to find out why, or think of another way around it ---------------------------------------------------------------------- balazsszekely · 2022-12-05 18:36 @petevick QuoteWell I have found out that line 7 is causing the delete not to work........ Ctrl1.ButtonColor:=StringToColor(str2);Most likely Ctrl1 is nil or StringToColor fails. ---------------------------------------------------------------------- JuhaManninen · 2022-12-05 18:37 Quote from: petevick on December 05, 2022, 04:56:50 pmCode: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i     for n:= idx-1 downto idx - 48 do    begin     if FindComponent(UndoObj(UndoList[n]).Cntrl) is TColorButton     then begin     Ctrl1:=self.FindComponent(UndoObj(UndoList[n]).Cntrl) as TColorButton;     str2:= UndoObj(UndoList[n]).CntrlVal;     Ctrl1.ButtonColor:=StringToColor(str2);     if UndoList.Count > 0 then UndoList.Delete(n);     end;    end; Not searching for the bug or knowing details of the types, I would clean the code a little. FindComponent is called twice and the ugly typecasts are repeated. This looks cleaner to me: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   for n := idx-1 downto idx - 48 do  begin    Obj := UndoObj(UndoList[n]);    Comp := FindComponent(Obj.Cntrl);    if Comp is TColorButton then    begin      TColorButton(Comp).ButtonColor := StringToColor(Obj.CntrlVal);      UndoList.Delete(n);    end;  end; ---------------------------------------------------------------------- petevick · 2022-12-05 19:23 Well I've found out what the problem was, in another part of the project, the UndoList was being added to in a OnColorChanged event triggered by line 7 - Ctrl1.ButtonColor:=StringToColor(str2); Prevented that happening and it's now working as I had hoped 3.5 hours ago. Thanks for all the replies, much appreciated. I can now clean the code up as advised. It certainly helped to stop me going around in a circle.