Recent

Author Topic: [SOLVED] TCheckListBox - Adding Objects causing crashes with Cocoa  (Read 2091 times)

Hansaplast

  • Hero Member
  • *****
  • Posts: 570
  • Tweaking4All.com
    • Tweaking4All

I one of my little projects, I'm using a TCheckListBox.
Since I have more data attached to each item, I created my own simple class with the additional information and I attach it to the items in the list.
However under Cocoa I keep getting crashes (SYSTEM$_$TOBJECT_$__$$_INHERITSFROM$TCLASS$$BOOLEAN), even though it works fine under Windows and Linux.
I suspect there is something wrong with Objects and the checkboxes in the CheckboxListbox under Cocoa - but it could also be my own mistake, so I'm trying to get verification in the forum before posting a bug report.


Attached a sample project, where I reproduced the issue in a very simple program.
The application works correct under Linux (GTK/2) and Windows.


Code: Pascal  [Select]
  1. TMyClass = class
  2.     something:integer;
  3.   end;
  4. ....
  5. CheckListBox1.Items.AddObject('some item text here',tmpMyClass);
  6. CheckListBox1.Checked[CheckListBox1.Count-1]:=true;
  7. ....
  8. procedure TForm1.CheckListBox1Click(Sender: TObject);
  9. begin
  10.   if (CheckListBox1.ItemIndex>-1) and (CheckListBox1.Items.Objects[CheckListBox1.ItemIndex]<>nil) then
  11.     begin
  12.       lblValue_Checklistbox.Caption:='value='+IntToStr(TMyClass(CheckListBox1.Items.Objects[CheckListBox1.ItemIndex]).something);
  13.       cbChecklistbox.Checked:=CheckListBox1.Checked[CheckListBox1.ItemIndex];
  14.     end;
  15. end;  
  16. ...
  17.  


This works perfectly fine under Linux (GTK/2) and Windows, but under Cocoa things will crash left, right and center.


When assigning "true", to the checked[] property, things crash (SYSTEM$_$TOBJECT_$__$$_INHERITSFROM$TCLASS$$BOOLEAN);
Code: Pascal  [Select]
  1. CheckListBox1.Checked[CheckListBox1.Count-1]:=true;



When assigning the value "false" the checkbox remains checked even though reading the value always results in "false".
If the user unchecks and checks the item I do get the same crash again.


I've also tried AddItem, but this results in the same problem.


Code: Pascal  [Select]
  1. CheckListBox1.AddItem('some item text here',tmpMyClass);


I suspect that the objects are used for the checked[] values. Either that or I'm doing it completely wrong (which is an option as well of course).


So what am I doing wrong, or is this indeed a bug in the Cocoa widgetset?
« Last Edit: November 24, 2018, 10:51:19 am by Hansaplast »


Hansaplast

  • Hero Member
  • *****
  • Posts: 570
  • Tweaking4All.com
    • Tweaking4All
Re: TCheckListBox - Adding Objects causing crashes with Cocoa
« Reply #2 on: November 23, 2018, 12:26:31 pm »
When looking in CocoaWSCheckLst.pas, I think I may have found confirmation that the checkbox value is indeed stored in the object property;


Code: Pascal  [Select]
  1. function TLCLCheckboxListCallback.SetCheckState(Index: Integer; AState: Integer;
  2.   InvalidateCocoa: Boolean = true): Boolean;
  3. begin
  4.   Result := Assigned(Strings) and (Index>=0) and (Index<strings.Count);
  5.   if not Result then Exit;
  6.   Result := strings.Objects[Index] <> TObject(PtrInt(AState));
  7.   if Result then
  8.   begin
  9.     strings.Objects[Index] := TObject(PtrInt(AState));
  10.     if InvalidateCocoa and Assigned(listview) then
  11.       listview.reloadDataForRow_column(Index, 0);
  12.   end;
  13. end;    

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2347
    • havefunsoft.com
Re: TCheckListBox - Adding Objects causing crashes with Cocoa
« Reply #3 on: November 24, 2018, 05:03:02 am »
when I saw that code, I was wondering when it would hit us back...
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

Hansaplast

  • Hero Member
  • *****
  • Posts: 570
  • Tweaking4All.com
    • Tweaking4All
Re: TCheckListBox - Adding Objects causing crashes with Cocoa
« Reply #4 on: November 24, 2018, 10:51:01 am »
Thanks for fixing the issue Dmitry!  :)