Recent

Author Topic: [SOLVED] TObjectList .delete() is not deleting  (Read 813 times)

petevick

  • Sr. Member
  • ****
  • Posts: 337
[SOLVED] TObjectList .delete() is not deleting
« on: December 05, 2022, 04:56:50 pm »
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][+][-]
  1.     for n:= idx-1 downto idx - 48 do
  2.     begin
  3.      if FindComponent(UndoObj(UndoList[n]).Cntrl) is TColorButton
  4.      then begin
  5.      Ctrl1:=self.FindComponent(UndoObj(UndoList[n]).Cntrl) as TColorButton;
  6.      str2:= UndoObj(UndoList[n]).CntrlVal;
  7.      Ctrl1.ButtonColor:=StringToColor(str2);
  8.      if UndoList.Count > 0 then UndoList.Delete(n);
  9.      end;
  10.     end;
  11.  
« Last Edit: December 05, 2022, 08:23:50 pm by petevick »
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

Handoko

  • Hero Member
  • *****
  • Posts: 5131
  • My goal: build my own game engine using Lazarus
Re: TObjectList .delete() is not deleting
« Reply #1 on: December 05, 2022, 05:14:47 pm »
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

  • Hero Member
  • *****
  • Posts: 6676
Re: TObjectList .delete() is not deleting
« Reply #2 on: December 05, 2022, 05:19:53 pm »
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
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TObjectList .delete() is not deleting
« Reply #3 on: December 05, 2022, 05:25:14 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.
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: TObjectList .delete() is not deleting
« Reply #4 on: December 05, 2022, 05:36:44 pm »
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.

Yes. But again: does .Count indicate that [n] actually exists? Are you sure that what you're trying to delete really is deletable?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TObjectList .delete() is not deleting
« Reply #5 on: December 05, 2022, 06:15:43 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.
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2010
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TObjectList .delete() is not deleting
« Reply #6 on: December 05, 2022, 06:24:44 pm »
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.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TObjectList .delete() is not deleting
« Reply #7 on: December 05, 2022, 07:09:32 pm »
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.
I'm sorry but I've been told this is as much of the code as I can publish, in any form.
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2010
  • Fifty shades of code.
    • Delphi & FreePascal
Re: TObjectList .delete() is not deleting
« Reply #8 on: December 05, 2022, 07:15:51 pm »
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.
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 O:-)
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TObjectList .delete() is not deleting
« Reply #9 on: December 05, 2022, 07:24:42 pm »
No need to be sorry and I hope you manage to find out where its doing something wrong O:-)
Well I have found out that line 7 is causing the delete not to work........
Code: Pascal  [Select][+][-]
  1.    Ctrl1.ButtonColor:=StringToColor(str2);
just need to find out why, or think of another way around it  :(
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

balazsszekely

  • Guest
Re: TObjectList .delete() is not deleting
« Reply #10 on: December 05, 2022, 07:36:31 pm »
@petevick
Quote
Well 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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: TObjectList .delete() is not deleting
« Reply #11 on: December 05, 2022, 07:37:21 pm »
Code: Pascal  [Select][+][-]
  1.     for n:= idx-1 downto idx - 48 do
  2.     begin
  3.      if FindComponent(UndoObj(UndoList[n]).Cntrl) is TColorButton
  4.      then begin
  5.      Ctrl1:=self.FindComponent(UndoObj(UndoList[n]).Cntrl) as TColorButton;
  6.      str2:= UndoObj(UndoList[n]).CntrlVal;
  7.      Ctrl1.ButtonColor:=StringToColor(str2);
  8.      if UndoList.Count > 0 then UndoList.Delete(n);
  9.      end;
  10.     end;
  11.  
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][+][-]
  1.   for n := idx-1 downto idx - 48 do
  2.   begin
  3.     Obj := UndoObj(UndoList[n]);
  4.     Comp := FindComponent(Obj.Cntrl);
  5.     if Comp is TColorButton then
  6.     begin
  7.       TColorButton(Comp).ButtonColor := StringToColor(Obj.CntrlVal);
  8.       UndoList.Delete(n);
  9.     end;
  10.   end;
  11.  
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

petevick

  • Sr. Member
  • ****
  • Posts: 337
Re: TObjectList .delete() is not deleting
« Reply #12 on: December 05, 2022, 08:23:28 pm »
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.  ;D ;D
Pete Vickerstaff
Linux Mint 21.1 Cinnamon, Lazarus 3.2, FPC 3.2.2

 

TinyPortal © 2005-2018