Recent

Author Topic: Is there a bug in TListBox?  (Read 8530 times)

vfclists

  • Hero Member
  • *****
  • Posts: 856
    • HowTos Considered Harmful?
Is there a bug in TListBox?
« on: May 03, 2010, 09:10:02 pm »

This code is to scan the list of selected items in a List box and add them to a string list. It crashes when it gets to an unselected item.

Am I doing something wrong here, or is it a known bug?

Code: [Select]
procedure TutilitiesInterfaceForm.GetEventsList(list: TStringList);
var
 i :integer;
begin
  for i := 0 to lbEventsToMonitorA.Items.Count - 1 do
  begin
    if lbEventsToMonitorA.Selected[i] = true then
      list.Add(lbEventsToMonitorA.Items[i]);
  end;
end;

Lazarus 2.1.0/FPC 3.2.0

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2565
Re: Is there a bug in TListBox?
« Reply #1 on: May 03, 2010, 11:56:05 pm »
on what line does it crash ?
This code should work.

ps.
Code: [Select]
if lbEventsToMonitorA.Selected[i] = true then
is the same as
Code: [Select]
if lbEventsToMonitorA.Selected[i] then
Selected[] already returns a boolean, no need to compare it against another boolean
« Last Edit: May 03, 2010, 11:58:20 pm by Marc »
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

vfclists

  • Hero Member
  • *****
  • Posts: 856
    • HowTos Considered Harmful?
Re: Is there a bug in TListBox?
« Reply #2 on: May 04, 2010, 01:45:58 am »
It crashes on this line:
Code: [Select]
if lbEventsToMonitorA.Selected[i] = true then
and the crash occurs when the item is not selected, ie always crashes on the first on selected item.

on what line does it crash ?
This code should work.

ps.
Code: [Select]
if lbEventsToMonitorA.Selected[i] = true then
is the same as
Code: [Select]
if lbEventsToMonitorA.Selected[i] then
Selected[] already returns a boolean, no need to compare it against another boolean

Lazarus 2.1.0/FPC 3.2.0

eny

  • Hero Member
  • *****
  • Posts: 1628
Re: Is there a bug in TListBox?
« Reply #3 on: May 04, 2010, 06:46:43 am »
Just a thought: is your listbox populated, the first time you call 'GetEventsList'?
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2565
Re: Is there a bug in TListBox?
« Reply #4 on: May 04, 2010, 08:50:21 am »
Just a thought: is your listbox populated, the first time you call 'GetEventsList'?

if not populated, "for i := 0 to lbEventsToMonitorA.Items.Count - 1 do" wont do anything.
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2565
Re: Is there a bug in TListBox?
« Reply #5 on: May 04, 2010, 08:51:16 am »
It crashes on this line:
Code: [Select]
if lbEventsToMonitorA.Selected[i] = true then
and the crash occurs when the item is not selected, ie always crashes on the first on selected item.

is lbEventsToMonitorA visible ?
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

vfclists

  • Hero Member
  • *****
  • Posts: 856
    • HowTos Considered Harmful?
Re: Is there a bug in TListBox?
« Reply #6 on: May 04, 2010, 09:17:34 am »
It is populated at design time. As I said, it crashes when the index hits the first unselected item.

Just a thought: is your listbox populated, the first time you call 'GetEventsList'?
Lazarus 2.1.0/FPC 3.2.0

vfclists

  • Hero Member
  • *****
  • Posts: 856
    • HowTos Considered Harmful?
Re: Is there a bug in TListBox?
« Reply #7 on: May 04, 2010, 09:19:18 am »
It crashes on this line:
Code: [Select]
if lbEventsToMonitorA.Selected[i] = true then
and the crash occurs when the item is not selected, ie always crashes on the first on selected item.

is lbEventsToMonitorA visible ?

It is visible, fully upfront. Something appears to be wrong with code generation.
Lazarus 2.1.0/FPC 3.2.0

Bart

  • Hero Member
  • *****
  • Posts: 4887
    • Bart en Mariska's Webstek
Re: Is there a bug in TListBox?
« Reply #8 on: May 04, 2010, 04:06:51 pm »
Similar code:
Code: [Select]
var
  i: Integer;
begin
  for i := 0 to listbox1.Items.Count - 1 do
    if listbox1.Selected[i] then
      debugln('Selected[',dbgs(i),'] = True, Value = ',Listbox1.Items[i])
    else
      debugln('Selected[',dbgs(i),'] = False, Value = ',Listbox1.Items[i]);
end;

runs perfectly fine here.

Q: Is list assigned (created) when you pass it to the procedure?

Bart

vfclists

  • Hero Member
  • *****
  • Posts: 856
    • HowTos Considered Harmful?
Re: Is there a bug in TListBox?
« Reply #9 on: May 05, 2010, 07:16:45 am »

I have checked and the code is correct. There were problems further down in the source code, but the debugger was executing them without moving the highlight to them.

The highlight would be in one place where as the code was executing somewhere else. Is that a known feature? (Lazarus 0.9.28 Windows)
Lazarus 2.1.0/FPC 3.2.0

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2565
Re: Is there a bug in TListBox?
« Reply #10 on: May 05, 2010, 09:46:46 pm »
Only if source and exe are out of sync. (iow, you edited the source without recompilation)
//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

 

TinyPortal © 2005-2018