[SOLVED] TShellTreeView node corruption lazarus / 31-LCL ---------------------------------------------------------------------- petevick · 2024-01-29 18:38 In the attached project is a mini folder browser that creates and deletes folders. When I create a folder in an empty folder I sometimes get a 'External:SIGSEGV' error, but it seems to be a bit random. It's triggered in the MenuNewFolderClick() procedure and starting at line 408, pNode seems to be corrupt after line 409. This happens in Linux and Windows. I'd be grateful for any help in tracking the problem. ---------------------------------------------------------------------- wp · 2024-01-29 18:46 Please remove third-party controls from the project and replace them by standard controls if their functionality is needed. ---------------------------------------------------------------------- petevick · 2024-01-29 19:53 Quote from: wp on January 29, 2024, 07:46:38 pmPlease remove third- party controls from the project and replace them by standard controls if their functionality is needed. I've replaced all the BGRA controls, but I'm still getting the random error, just not as often, and I'm still unable to track why it happens. The error still happens in Linux and Windows. ---------------------------------------------------------------------- dsiders · 2024-01-29 20:32 Quote from: petevick on January 29, 2024, 08:53:09 pmQuote from: wp on January 29, 2024, 07:46:38 pmPlease remove third-party controls from the project and replace them by standard controls if their functionality is needed. I've replaced all the BGRA controls, but I'm still getting the random error, just not as often, and I'm still unable to track why it happens. The error still happens in Linux and Windows. I'll bet you don't have a directory called: C:\home\Pete\C:\ ---------------------------------------------------------------------- petevick · 2024-01-29 20:38 Quote from: dsiders on January 29, 2024, 09:32:27 pmI'll bet you don't have a directory called: C:\home\Pete\C:\ You're not wrong, it's what happens when you swap from Windows to Linux ---------------------------------------------------------------------- wp · 2024-01-29 22:35 Yes, I saw some crashes, it seems they occur when the "if pNode.HasChildren" condition is false. In this case you repopulate the tree starting at the parent of pnode. But this is critical since it erases the old pnode and creates a new one. Therefore, pnode which you had determined at the beginning of the procedure is no longer valid. I'd store the path to the new node in a variable (newPath := mytreeView.path + TmpName;) and then simply call the FindNodeWithPath method of the tree's Items to get to the newly created node. 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     newPath := mytreeView.path + TmpName;     pNode:=myTreeView.Selected;    if CreateDir(newPath) then    begin      pNode.Expanded:=False;      pNode.Expanded:=True;      if pNode.HasChildren then        pNode:= pNode.FindNode(TmpName)      else      begin        pNode.Parent.Expanded := false;        pNode.Parent.Expanded := true;        pNode := myTreeView.Items.FindNodeWithTextPath(newPath);      end;      pNode.Selected:=True;    end; Another (less important) issue is the check "if OSVersion = 'Win'" since your OSVersion() function returns the string 'Windows' rather than 'Win'. ---------------------------------------------------------------------- petevick · 2024-01-30 07:39 Quote from: wp on January 29, 2024, 11:35:57 pmYes, I saw some crashes, it seems they occur when the "if pNode.HasChildren" condition is false. In this case you repopulate the tree starting at the parent of pnode. But this is critical since it erases the old pnode and creates a new one. Therefore, pnode which you had determined at the beginning of the procedure is no longer valid. I'd store the path to the new node in a variable (newPath := mytreeView.path + TmpName;) and then simply call the FindNodeWithPath method of the tree's Items to get to the newly created node. 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     newPath := mytreeView.path + TmpName;     pNode:=myTreeView.Selected;    if CreateDir(newPath) then    begin      pNode.Expanded:=False;      pNode.Expanded:=True;      if pNode.HasChildren then        pNode:= pNode.FindNode(TmpName)      else      begin        pNode.Parent.Expanded := false;        pNode.Parent.Expanded := true;        pNode := myTreeView.Items.FindNodeWithTextPath(newPath);      end;      pNode.Selected:=True;    end; Another (less important) issue is the check "if OSVersion = 'Win'" since your OSVersion() function returns the string 'Windows' rather than 'Win'. Unfortunately that still generates errors for me. Line 14 equates to Nil in both Linux and Windows and then obviously line 16 gives the error. Full procedure... 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 procedure TForm1.MenuNewFolderClick(Sender: TObject);var  pNode: TTreeNode;  TmpName: String = '';  NewPath: String = '';  n: Integer = 1;begin  if myTreeView.Selected <> Nil then  begin    if OSVersion = 'Windows' then begin     TmpName:='New folder';     While DirectoryExists(myTreeView.Path+TmpName) do       begin       TmpName:= 'New folder ('+IntToStr(n)+')';       n:= n+1;       end;  end else begin      TmpName:='Untitled Folder';      While DirectoryExists(myTreeView.Path+TmpName) do        begin        TmpName:= 'Untitled Folder '+IntToStr(n);        n:= n+1;        end;    end;   NewPath := mytreeView.path + TmpName;   pNode:=myTreeView.Selected;   if CreateDir(myTreeView.Path+TmpName) then   begin    pNode.Expanded:=False;    pNode.Expanded:=True;    if pNode.HasChildren then      pNode:= pNode.FindNode(TmpName)    else  begin      pNode.Parent.Expanded := false;      pNode.Parent.Expanded := true;      pNode := myTreeView.Items.FindNodeWithTextPath(NewPath);    end;    pNode.Selected:=True;    end;   end;end; ---------------------------------------------------------------------- wp · 2024-01-30 09:35 Tested on Windows (Laz/main) and Mint (Laz/3.0) - no problem here... Set a breakpoint at the guilty line with FindNodeWithTextPath and check what's in "newPath". Sometimes directories have a trailing (back)slash (although I cannot imagine how it could get there). What are the path delimiters? There was an issue because the path delimiter is inherited from the treeview ancestor which always uses a forward slash - this will cause a problem in Windows which wants a backslash (IIRC, this was fixed in Laz 3.0). ---------------------------------------------------------------------- petevick · 2024-01-30 11:02 Quote from: wp on January 30, 2024, 10:35:02 amTested on Windows (Laz/main) and Mint (Laz/3.0) - no problem here... Set a breakpoint at the guilty line with FindNodeWithTextPath and check what's in "newPath". Sometimes directories have a trailing (back)slash (although I cannot imagine how it could get there). What are the path delimiters? There was an issue because the path delimiter is inherited from the treeview ancestor which always uses a forward slash - this will cause a problem in Windows which wants a backslash (IIRC, this was fixed in Laz 3.0). Checked newPath in Linux and Windows and the path and delimiters are correct, there is also no trailing delimiter. I also tried with a trailing delimiter, but still got pNode = Nil. I've never had any success using FindNodeWithTextPath() so I've avoiding using it. Is it remotely possible that I'm not using the latest FindNodeWithTextPath(), even though I'm using Laz 3.0, how would I check ?. ---------------------------------------------------------------------- petevick · 2024-01-30 16:56 @wp - I think I've finally cracked this. I decided to use your method for creating a folder that was in the project you posted here - https: //forum.lazarus.freepascal.org/index.php/topic,65978.msg503556.html#ms g503556. In my project it worked without error in Linux but I was getting errors in Windows, and auxiliary drives in the tree where disappearing. I was about to give up on it when I had a light bulb moment, in my project the ShellTreeView.Root value was C:\, I checked with your project and it was blank, I changed the value in my project to blank and all the errors went away, plus I get to keep the nice BGRA components  For completeness, this is the final procedure..... 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 procedure TForm1.MenuNewFolderClick(Sender: TObject);var  selNode: TTreeNode;  selDir: String;  newDir: String;  TmpName: String;  n: Integer = 1;begin  selNode := myTreeView.Selected;  if selNode <> nil then  begin    if OSVersion = 'Windows' then begin     TmpName:='New folder';     While DirectoryExists(myTreeView.Path+TmpName) do       begin       TmpName:= 'New folder ('+IntToStr(n)+')';       n:= n+1;       end;    end else begin      TmpName:='Untitled Folder';      While DirectoryExists(myTreeView.Path+TmpName) do        begin        TmpName:= 'Untitled Folder '+IntToStr(n);        n:= n+1;        end;    end;    selDir := myTreeView.GetPathFromNode(selNode);    newDir := selDir + TmpName;    CreateDir(newDir);    UpdateShellTreeView(myTreeView, selDir);    myTreeView.Path := newDir;  end else    ShowMessage('Please select the folder to which the new folder should be added.');end;  I'll probably sleep a bit better tonight now. Thanks for all the help you've given me wp, I really do appreciate it, ---------------------------------------------------------------------- wp · 2024-01-30 17:03 Yeah, I was about to write the same... Just had to fix a small bug in the Laz/main TShellTreeView.UpdateView. 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 procedure TForm1.MenuNewFolderClick(Sender: TObject);var  oldPath: String;  newPath: String;  newName: String;  TmpName: String;  n: Integer = 1;begin  oldPath := myTreeView.Path;  if OSVersion = 'Windows' then  newName := 'New folder'  else    newName := 'Untitled folder';  TmpName := newName;  While DirectoryExists(oldPath+TmpName) do  begin    TmpName := Format('%s (%d)', [newName, n]);    n:= n+1;  end;   newPath := oldpath + TmpName;  if CreateDir(newPath) then  begin    UpdateShellTreeView(mytreeView, oldPath);    myTreeView.Path := newPath;   end;end; ---------------------------------------------------------------------- petevick · 2024-01-30 17:15 Quote from: wp on January 30, 2024, 06:03:50 pmYeah, I was about to write the same... Just had to fix a small bug in the Laz/main TShellTreeView.UpdateView. 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 procedure TForm1.MenuNewFolderClick(Sender: TObject);var  oldPath: String;  newPath: String;  newName: String;  TmpName: String;  n: Integer = 1;begin  oldPath := myTreeView.Path;  if OSVersion = 'Windows' then  newName := 'New folder'  else    newName := 'Untitled folder';  TmpName := newName;  While DirectoryExists(oldPath+TmpName) do  begin    TmpName := Format('%s (%d)', [newName, n]);    n:= n+1;  end;   newPath := oldpath + TmpName;  if CreateDir(newPath) then  begin    UpdateShellTreeView(mytreeView, oldPath);    myTreeView.Path := newPath;   end;end; Brilliant, even simpler !! ---------------------------------------------------------------------- petevick · 2024-01-30 18:33 Quote from: wp on January 30, 2024, 06:03:50 pmYeah, I was about to write the same... Just had to fix a small bug in the Laz/main TShellTreeView.UpdateView. wp, would I simply copy the shellctrls.pas file from git to my lcl folder ?? ---------------------------------------------------------------------- wp · 2024-01-30 18:54 You can try, but there is a high probability that the ShellCtrls of Laz/main depends on changes in other units of main, and then you will not be able to compile that mix-up with your Laz 2.2.6 any more. Therefore, absolutely necessary before such experiments: always make a backup copy of the changed file(s) so that you can always return to the starting point. The recommended way would be to install Laz/3.0 and to patch ShellCtrls with the two tiny changes I had to make in Shelltreview.UpdateView. Or even better, to install Laz/fixes from source (the new changes are not yet in there, but I can take care of speeding it up). ---------------------------------------------------------------------- petevick · 2024-01-30 19:43 Quote from: wp on January 30, 2024, 07:54:07 pmYou can try, but there is a high probability that the ShellCtrls of Laz/main depends on changes in other units of main, and then you will not be able to compile that mix-up with your Laz 2.2.6 any more. Therefore, absolutely necessary before such experiments: always make a backup copy of the changed file(s) so that you can always return to the starting point. The recommended way would be to install Laz/3.0 and to patch ShellCtrls with the two tiny changes I had to make in Shelltreview.UpdateView. Or even better, to install Laz/fixes from source (the new changes are not yet in there, but I can take care of speeding it up). How would I go about installing Laz/fixes. From searching for Laz/fixes I came across FPCUpDeluxe, would that be a better option ?. ---------------------------------------------------------------------- wp · 2024-01-30 21:30 I am sorry: I looked more carefully now, TShellTreeView.UpdateView is a new feature, and thus has not been back-ported to FIXES, it is in the MAIN branch only.