@@ -4397,9 +4397,11 @@ procedure TMainForm.SessionConnect(Sender: TObject);
43974397 for i:=High(FTreeClickHistory) downto Low(FTreeClickHistory) do begin
43984398 if FTreeClickHistory[i] <> nil then begin
43994399 DBObj := DBtree.GetNodeData(FTreeClickHistory[i]);
4400- if (DBObj = nil) or (DBObj.Connection = nil) or (not DBObj.Connection.Active) then // Session disconnected
4400+ // Node data can be nil when the node was freed without triggering OnFreeNode,
4401+ // which VirtualTrees does not do for uninitialized nodes. See issue #2651
4402+ if (not Assigned(DBObj^)) or (DBObj^.Connection = nil) or (not DBObj^.Connection.Active) then // Session disconnected
44014403 Break;
4402- if DBObj.Connection.Parameters.SessionPath = SessionPath then begin
4404+ if DBObj^ .Connection.Parameters.SessionPath = SessionPath then begin
44034405 Node := FTreeClickHistory[i];
44044406 break;
44054407 end;
@@ -4410,7 +4412,8 @@ procedure TMainForm.SessionConnect(Sender: TObject);
44104412 SessionNode := DBtree.GetFirstChild(nil);
44114413 while Assigned(SessionNode) do begin
44124414 DBObj := DBtree.GetNodeData(SessionNode);
4413- if DBObj.Connection.Parameters.SessionPath = SessionPath then begin
4415+ if Assigned(DBObj^) and Assigned(DBObj^.Connection)
4416+ and (DBObj^.Connection.Parameters.SessionPath = SessionPath) then begin
44144417 Node := SessionNode;
44154418 end;
44164419 SessionNode := DBtree.GetNextSibling(SessionNode);
@@ -10579,6 +10582,11 @@ procedure TMainForm.RefreshTree(FocusNewObject: TDBObject=nil);
1057910582 DBNode := DBtree.GetNextSibling(DBNode);
1058010583 end;
1058110584 end;
10585+ // ResetNode clears vsInitialized, and VirtualTrees does not call OnFreeNode
10586+ // for uninitialized nodes when they get freed. That would leave dangling node
10587+ // pointers in FTreeClickHistory. Keep the node initialized, also when collapsed.
10588+ // See issue #2651
10589+ DBtree.ValidateNode(SessNode, False);
1058210590 end;
1058310591 end else begin
1058410592 DBNode := FindDbNode(DBtree, FocusNewObject.Connection, FocusNewObject.Database);
0 commit comments