Recent

Author Topic: Access violation whenever treeview node selection was tested  (Read 358 times)

chiong

  • Newbie
  • Posts: 2
Access violation whenever treeview node selection was tested
« on: November 05, 2024, 03:31:33 pm »
I am trying to develope a software to do schueller's tissue salt repertory. I intent to make the software freeware.

I am stucked at the point where I kept getting access violation whenever the button to copy the selected symptom from the treeview to the selection listview. The code tested if any selection was made at any of the treeview nodes.

The error was Access Violation .... $000000000000818. .... treeview.inc at line 4803.

I have attached the full code. Any help would be much appreciated. I took a long time typing in the schueller's reprtory table. It is free to be use by anyone. Tissue salts are tissue remedies for a whole range of diseases. I had used it often and I found it very effective even for very serious diseases.  Thank you for your kind attention.

Fibonacci

  • Hero Member
  • *****
  • Posts: 615
  • Internal Error Hunter
Re: Access violation whenever treeview node selection was tested
« Reply #1 on: November 05, 2024, 04:11:03 pm »
Your TreeView must have the same name that is stored in SchuellerRP.dat, that is "tv_eg1".

Thats the first issue. The second is you cant call ".Selected" on nil, so add a nil check:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnSelectClick(Sender: TObject);
  2. begin
  3.   if (tv_eg1.Selected = nil) or (tv_eg1.Selected.Selected <> true) then begin
  4.     Showmessage('Please select a symptom-remedy to add to list');
  5.     exit;
  6.   end;
  7.  
  8.   ShowMessage('OK');
  9. end;

I guess with the nil check on .Selected you can remove ".Selected" check on .Selected item. I dont know exactly how TreeViews work, but now you dont have AV ;D

Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnSelectClick(Sender: TObject);
  2. begin
  3.   if tv_eg1.Selected = nil then begin
  4.     Showmessage('Please select a symptom-remedy to add to list');
  5.     exit;
  6.   end;
  7.  
  8.   ShowMessage('OK');
  9. end;
« Last Edit: November 05, 2024, 04:54:18 pm by Fibonacci »

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Access violation whenever treeview node selection was tested
« Reply #2 on: November 05, 2024, 04:56:04 pm »
Your TreeView must have the same name that is stored in SchuellerRP.dat, that is "tv_eg1"

* Mac Mini M1
* macOS 14.6.1
* Lazarus 3.99
* FPC 3.3.1

The data in SchuellerRP.dat loads okay, although I did have to hard code the full path to get it to load. The problem comes with the code in
Code: Pascal  [Select][+][-]
  1. procedure TForm1.btnSelectClick(Sender: TObject);
  2. begin
  3.   if SchuellerTreeV.Selected.Selected <> True then  // <== AVs here
  4.   begin
  5.     ShowMessage('Please select a symptom-remedy to add to list');
  6.     exit;
  7.   end;
  8. end;
  9.  

I haven't yet investigated that because I have been distracted by an apparent bug in 3.99 for macOS aarch64...

UPDATE: this code works when the app is run without debugging because ParamStr(0) returns the app bundle path (/Users/carlcaulkett/Code/fpc/SchuellersBook/SchuellersRepertory.app) in this case, and the ExtractFileDir is able to obtain the app folder.
However, if run with debugging, ParamStr(0) returns the path of the internal Unix executable (/Users/carlcaulkett/Code/fpc/SchuellersBook/SchuellersRepertory.app/Contents/MacOS/SchuellersRepertory) in this case.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   F: TFileStream;
  4.   DataPath: string;
  5. begin
  6.   DataPath := ExtractFileDir(ParamStr(0)) + DirectorySeparator + 'SchuellerRP.dat';
  7.   F := TFileStream.Create(DataPath, fmOpenRead or fmShareDenyWrite);
  8.   try
  9.     F.ReadComponent(SchuellerTreeV);
  10.   finally
  11.     F.Free;
  12.   end;
  13. end;
  14.  

"It builds... ship it!"

Fibonacci

  • Hero Member
  • *****
  • Posts: 615
  • Internal Error Hunter
Re: Access violation whenever treeview node selection was tested
« Reply #3 on: November 05, 2024, 05:01:00 pm »
The data in SchuellerRP.dat loads okay

Loads okay, but you cant access the component, it becomes corrupted. Try changing .Width of the TreeView, thats an AV.

carl_caulkett

  • Hero Member
  • *****
  • Posts: 649
Re: Access violation whenever treeview node selection was tested
« Reply #4 on: November 05, 2024, 05:04:59 pm »
The data in SchuellerRP.dat loads okay

Loads okay, but you cant access the component, it becomes corrupted. Try changing .Width of the TreeView, thats an AV.

As you correctly pointed out, the TTreeView has to have the same name as is contained in the .DAT file, namely tv_eg1. Presumably this removes the AV if you resize it...
"It builds... ship it!"

chiong

  • Newbie
  • Posts: 2
Re: Access violation whenever treeview node selection was tested
« Reply #5 on: November 06, 2024, 12:50:30 pm »
Thank you all. I had rebuild the dat file by changing the treeview name. I actually wrote another app to populate the treeview from a text file of the schueller's repertory. From there I saved it through a filestream. And in the app that I had this access violation I loaded the data through a filestream as well.

Now that the treeview name in both app are the same I am able to perform the selected test without triggering the access violation.

Again thank you all for answering.

 

TinyPortal © 2005-2018