TTMSFNCGanttChart 1.3.3.13 - deleting the selected task: access violation in EndUpdate, stale list selection; links to deleted tasks, OnGanttTaskDelete, PageDown

Hello,

Delphi 13.1 Florence, VCL, Win64, TMS FNC Gantt Chart 1.3.3.13, TMS FNC Core 4.5.0.3.

While working with the task list we ran into an access violation that we traced back to its selection, and noticed three more things along the way. All four are in the attached demo GanttSelectionDelete.zip. It runs by itself after start and writes a log next to the exe. Apart from test 1b, where TMS itself raises the exception, the demo never dereferences a pointer that may be stale; it only compares pointer values with the current node structure and task tree.

A note first, so that you can judge the first point correctly. In our application we switch on multi-selection of the task list (Interaction.MultiSelect with msSiblingOnly). TaskList is protected, so we reach it through an access class (a descendant of TTMSFNCGanttChart). We also take over OnAfterSelectNode of the list, because the chart's own handler (DoOnNodeSelect) reduces every selection to one task. We know that the chart is built around one selected task. The demo therefore uses the default settings everywhere (single selection, public methods only), except test 1c, which shows our setup. Points 2 to 4 do not depend on the selection at all.

1. Deleting the selected task: access violation in EndUpdate, and the task list keeps the old node in its selection

Demo, test 1b, default settings: nine tasks (R1, R2, P with P.1–P.4, R3, R4), SelectTask(R2), then

GanttChart.BeginUpdate;
GanttChart.DeleteTask(R2);
GanttChart.EndUpdate;   // EAccessViolation

EndUpdate raises EAccessViolation ("read of address FFFFFFFFFFFFFFFF"). Call stack (screenshot Test1b_v2_callstack_AV.png):

TObject.InheritsFrom + $8
@IsClass + $11
TTMSFNCCustomGanttChart.GetSelectedRow + $78
TTMSFNCCustomGanttChart.UpdateTimeLineViewDates
TTMSFNCCustomGanttChart.UpdateGanttTimelineSettings
TTMSFNCCustomGanttChart.UpdateGanttTimeline + $47
TTMSFNCCustomGanttChart.EndUpdate + $90

Nothing takes the deleted task out of the timeline selection before the chart looks at it again. TTMSFNCCustomGanttChart.ProjectTaskDelete (VCL.TMSFNCGanttChart.pas, line 8336) only calls the adapter and fires OnGanttTaskDelete, and inside BeginUpdate it skips even that (see point 3). The selected timeline item therefore still has the freed task as its DataObject. EndUpdate → UpdateGanttTimeline (line 8556) → UpdateGanttTimelineSettings → UpdateTimeLineViewDates (line 8779) → GetSelectedRow then runs DataObject is TTMSFNCGanttTask (line 6075) on freed memory. GetSelectedTask (line 6091, called from UpdateGanttTimelineItems, line 8705) has the same check. Reading the code, a delete without BeginUpdate takes the same route right away (ProjectTaskDeleted → UpdateGanttChartItems → UpdateGanttTimelineItems → SelectedTask); see test 1d below.

After the exception, the list selection still holds the freed node of R2. In all our runs, its address had already been reused for the node of R3, so R3 was shown as selected, and the focused node was nil. The timeline stayed empty for the rest of the run; we did not look into why. Test 1a is the control: the same steps without DeleteTask. There the selection is restored correctly in the list and in the timeline.

Test 1d repeats 1b without BeginUpdate, on a second chart with default settings (after the exception in 1b the timeline of the first chart stays empty). In two runs there was no exception. The list selection, however, again holds the old pointer, which now belongs to the node of R3, and the focused node is nil, the same as after 1b. So the list part does not depend on BeginUpdate. That no exception was raised does not show that no freed memory was read: whether is fails depends on what the memory manager has put into the block.

The list part has its own cause. TTMSFNCCustomGanttChart.EndUpdate (line 10111) calls FTaskList.ClearNodes (so does UpdateGanttListItems when the structure has changed, line 8434, which is the path of test 1d), and TTMSFNCGanttListData.ClearNodes (VCL.TMSFNCGanttListData.pas, line 2885) resets the focused node, but not the selection:

procedure TTMSFNCGanttListData.ClearNodes;
begin
  BeginUpdate;
  NodeListBuild := False;
  Nodes.Clear;
  ClearFocusedNode(nil);
  EndUpdate;
end;

TTMSFNCCustomGanttList.ClearFocusedNode (VCL.TMSFNCCustomGanttList.pas, line 3068) only sets FFocusedNode := nil. The rebuild then runs BuildNodeList, and FNodeStructure.Clear (VCL.TMSFNCGanttListData.pas, line 3532) frees every virtual node, because TTMSFNCGanttListNodeStructure is a TObjectList<TTMSFNCGanttListVirtualNode>. FSelectedNodes still points to these nodes. The selection is restored only through the timeline (UpdateGanttTimelineItems → SelectTask(selTask) → SelectVirtualNode, which clears FSelectedNodes first). When the timeline has no valid selected task, nothing touches the list selection. The path that removes a single node already does both: RemoveNodeInternal (VCL.TMSFNCGanttListData.pas, lines 4442 and 4457–4459) calls ClearFocusedNode(ANode) and RemoveNodeFromSelection(ANode). Only the clear-all paths ClearNodes and ClearNodeList (line 2877) are missing the second half.

TMS reads the stale list entries itself as well:

  • DoOnTaskListBeforeDrawNodeText (VCL.TMSFNCGanttChart.pas, line 5368) compares FTaskList.SelectedNodes[I] = ANode.Node for every cell it draws, and GetSelNode (VCL.TMSFNCCustomGanttList.pas, line 3337) returns .Node of the freed virtual node.
  • HandleKeyDown reads Row of the first and the last entry of FSelectedNodes for Shift+arrow (line 7731 ff.).

With our multi-selection the list part shows up without any delete. Test 1c selects the siblings P.2, P.3 and P.4 with SelectVirtualNodes and then calls BeginUpdate/EndUpdate without any change. Afterwards the three entries point to the nodes of P.1, P and R2. The list shows these three rows as selected, on two levels (screenshot Test1c_v2_siblings_selection_after_EndUpdate.png), and a delete command working on SelectedNodes would delete the wrong tasks. In our application the memory had once been reused for something else, and our code got an access violation (also "read of address FFFFFFFFFFFFFFFF") the next time it looked at the DataObject of SelectedNodes[0].

Suggestions:

  • When a task is deleted, remove its timeline item from the timeline selection right away, also inside BeginUpdate. Alternatively, check that the selected task still belongs to the project before GetSelectedRow/GetSelectedTask look at it.
  • Clear the list selection together with the focus, for example:
procedure TTMSFNCCustomGanttList.ClearFocusedNode(ANode: TTMSFNCGanttListVirtualNode);
begin
  if (ANode = FFocusedNode) or (ANode = nil) then
    FFocusedNode := nil;
  if ANode = nil then
    FSelectedNodes.Clear;
end;

ClearFocusedNode(nil) is called only from ClearNodes and ClearNodeList (lines 2881 and 2890), so this would not change the single-node path. Even better: remember the DataObject of the selected nodes and of the focused node before the rebuild, and select them again afterwards, as the timeline path already does for one task.

Until then, we clear the selection (SelectTask(nil)) before we delete tasks, and we drop the whole list selection after each outer EndUpdate when no valid focused node is left.

2. DeleteTask leaves the link of the successor pointing to the deleted task

TTMSFNCGanttTask.Destroy (VCL.TMSFNCGanttClasses.pas, line 2844) clears its own TaskDependencies (line 2856), that is, its links to its predecessors. Links of other tasks that point to the destroyed task stay. TTMSFNCGanttProject.RemoveDependentTask (line 5038) would remove them, but nothing calls it: TTMSFNCGanttProject.DeleteTask (line 4438) only deletes the collection item.

Demo, test 4: A, and B with a finish-to-start link on A. Inside BeginUpdate/EndUpdate: DeleteTask(A), then a new, unrelated task N, four weeks later:

before:  A=0000020ED1D3B980  B.TaskDependencies.Count=1  DependsOnTask=0000020ED1D3B980  B starts Thu 2026-11-05
after DeleteTask(A):  B.TaskDependencies.Count=1  DependsOnTask=0000020ED1D3B980  same as the deleted A: yes
new task N=0000020ED1D3B980  N got the address of the deleted A: yes
After CalculateTasks: B starts Wed 2026-12-02 (before: Thu 2026-11-05), N ends Wed 2026-12-02

So B keeps a link to freed memory. As soon as the memory manager hands that block to a new task, B silently follows a task that nobody linked to it, and the timeline draws a link line from N to B (screenshot Test4_link_N_to_B.png). Reading the code, the same happens to links on subtasks when a parent task is deleted. We also saw it in our application when Assign rebuilds the subtasks of a parent task. We now remove these links ourselves before every delete. Would it make sense for Destroy (or DeleteTask) to remove them, for example via RemoveDependentTask?

3. OnGanttTaskDelete is not fired inside BeginUpdate/EndUpdate, by ClearTasks, or for subtasks

The documentation says "Occurs before a task is deleted from the project." Demo, test 2:

2a  DeleteTask(X), no BeginUpdate:                         fired 1 time(s)  [X]
2b  DeleteTask(X) inside BeginUpdate/EndUpdate:            fired 0 time(s)  []
2c  Project.ClearTasks with X and Y:                       fired 0 time(s)  []
2d  DeleteTask(P) with subtasks P.1, P.2, no BeginUpdate:  fired 1 time(s)  [P]

The event is blocked in two places:

  • TTMSFNCGanttProject.DoInternalTaskDelete (VCL.TMSFNCGanttClasses.pas, line 4584) only notifies when FUpdating is False.
  • TTMSFNCCustomGanttChart.ProjectTaskDelete (VCL.TMSFNCGanttChart.pas, line 8338) only fires the event when FUpdateCount = 0.

BeginUpdate of the chart sets both (line 3421 calls Project.BeginUpdate). TTMSFNCGanttTasks.Clear (VCL.TMSFNCGanttClasses.pas, line 3593) sets FUpdating by itself. TTMSFNCGanttTask.Destroy uses it for the subtasks (FSubTasks.Clear, line 2855), so subtasks are never reported, even without BeginUpdate.

An application that keeps its own data per task (for example in DataPointer) therefore cannot rely on the event to release it. Most deletes happen inside BeginUpdate/EndUpdate, so the event does not come for them. Is this intended? If so, could the documentation say so? Otherwise, could the event be fired for every deleted task, including subtasks? Reading the code, the call FAdapter.DeleteTask(ATask) sits in the same branch of ProjectTaskDelete. We do not use an adapter, so we have not checked whether it catches up later.

4. PageDown/PageUp in the task list moves too little when a parent task is collapsed

TTMSFNCCustomGanttList.HandleKeyDown computes the new row as sel.Row + (StopRow - StartRow) for PageDown (line 7672) and sel.Row - (StopRow - StartRow) for PageUp (line 7655). The two values do not count the same thing:

  • Row is the index in NodeStructure, which includes the subtasks of collapsed parents.
  • StartRow/StopRow count visible rows (TTMSFNCGanttListBase.UpdateVisualRange, line 1188 ff., and GetVisibleNodeForIndex).

If the computed index lands on a hidden subtask, GetNextFocusableNode takes the next visible node. With a collapsed parent above, PageDown therefore jumps much less than a page. Reading the code, Shift+PageDown goes through the same calculation.

Demo, test 3 (default settings): a parent task P with 40 subtasks, followed by 40 tasks R01–R40, SelectTask(P), PageDown:

P collapsed:  page = 10 rows, visible rows=41
  focus on visible row 0 (P, Row=0), PageDown -> visible row 1 (R01, Row=41)   one page down would be visible row 10 (R10)
P expanded (for comparison):  page = 10 rows, visible rows=81
  focus on visible row 0 (P, Row=0), PageDown -> visible row 10 (P.10, Row=10)   one page down would be visible row 10 (P.10)

Suggestion: compute the target in visible rows, for example the index of sel in VisibleNodes plus or minus the page size (limited to the list), and then the next selectable node from there.

Thanks!

GanttSelectionDelete.zip (10.0 KB)

Hello,

a follow-up to point 1, on the same code path (the rebuild of the task list in EndUpdate), this time about the vertical scroll position.

If BeginUpdate/EndUpdate runs while the task list is scrolled down, the task list jumps back to the top, but the timeline keeps its position. Task list and timeline are then out of sync: the rows of the list no longer match the bars until the user scrolls one of the two. No change to the project is needed; BeginUpdate followed by EndUpdate is enough.

The attached GanttSelectionDelete_v4.zip has a new test 5 (default settings, 80 tasks, on the separate chart on the right). The task list is scrolled down through its scroll bar, the timeline follows, then BeginUpdate/EndUpdate is called without any change:

after adding 80 tasks:         task list: Position=   0 (Max 2401, PageSize 314), first visible row  0 (T01)   timeline: Position=   0 (Max 2400, PageSize 314)
list scrolled to 1200:         task list: Position=1200 (Max 2401, PageSize 314), first visible row 40 (T41)   timeline: Position=1200 (Max 2400, PageSize 314)
after BeginUpdate/EndUpdate:   task list: Position=   0 (Max 2401, PageSize 314), first visible row  0 (T01)   timeline: Position=1200 (Max 2400, PageSize 314)   <- task list and timeline differ

The button "Test 5, keep the state" runs test 5 alone and leaves the chart as it is afterwards.

Reading the code of 1.3.3.13, this is what happens:

  1. TTMSFNCCustomGanttChart.EndUpdate (VCL.TMSFNCGanttChart.pas, line 10100) calls FTaskList.ClearNodes and then UpdateGanttList.
  2. TTMSFNCGanttListData.ClearNodes (line 2885) has its own BeginUpdate/EndUpdate. Its EndUpdate (VCL.TMSFNCGanttListBase.pas, line 694) runs UpdateGanttListCache → UpdateScrollBars while the list is empty.
  3. With an empty list, UpdateScrollBars sets PageSize and Max of the vertical scroll bar to 0, and vs.Position := Min(vs.Position, vs.Max) (line 1510) sets the position to 0.
  4. UpdateScrollBars sets FBlockScrollingUpdate first, so VScrollChanged (line 683) returns early. VerticalScrollPositionChanged → DoVScroll is not called, and DoOnTaskListScroll never moves the timeline.
  5. The list is then filled again and stays at the top. The timeline is refilled as well but keeps its position (1200 in the log).

Suggestion: keep the vertical position of the task list across the rebuild in EndUpdate (remember it before ClearNodes and set it again after UpdateGanttList, limited to the new range), or at least set the timeline to the position of the list afterwards.

Our workaround: we remember the position of the task list before the outer BeginUpdate, set it again after EndUpdate and then set the timeline to the same value.

Thanks!

GanttSelectionDelete_v4.zip (10.8 KB)

Hi,

Thank you for the detailed report, improvements and fixes for these issues will be part of the upcoming v1.3.3.14.

Thanks, Tünde. I installed 1.3.3.14 (together with FNC Core 4.5.0.7), rebuilt the demo from this thread (GanttSelectionDelete v4) and tested our application. Everything we reported is fixed:

  1. Access violation after deleting the selected task: test 1b of the demo runs without an exception now. In our own tests, the case behind the original crash (select a task, Ctrl+A on its siblings, switch on our critical path view) no longer crashes. Deleting one task, two selected siblings, a parent task with a subtask, and cancelling a newly inserted subtask all run without an exception. So do the paths that clear all tasks: switching the calendar, and closing and reopening a plan.
  2. Stale list selection: in tests 1b, 1c and 1d the list selection is empty after the rebuild, instead of pointing to the nodes of other tasks (1.3.3.13: 1 of 1, 3 of 3 and 1 of 1 entries on a different task). So a sibling multi-selection without a timeline selection is now cleared by BeginUpdate/EndUpdate, which is fine for us. In our application, our own clean-up of such a selection found nothing to discard in the case I checked in the debugger.
  3. Links to a deleted task: in test 4, B has no link any more after DeleteTask(A), even though the next new task again got the address of A (1.3.3.13: B silently followed the new task).
  4. OnGanttTaskDelete: test 2 gives 1 call without an update, 1 inside BeginUpdate/EndUpdate, 2 for ClearTasks with two tasks, and 3 for a parent with two subtasks, parent first (1.3.3.13: 1, 0, 0, 1). Our handler frees our per-task data and can be called more than once, so there is no conflict with our own clean-up.
  5. PageDown/PageUp: in test 3, PageDown with the collapsed parent now lands on R10 instead of R01. In our application it moves a full page in both directions.
  6. Vertical position after BeginUpdate/EndUpdate: in test 5 the task list stays at 1200 together with the timeline (1.3.3.13: list 0, timeline 1200). In our application the list and the timeline stay in sync, including after our edit dialog. We still restore the position ourselves afterwards, because we collapse parent tasks again after the rebuild.

Thank you for the quick and complete fixes!

One more observation from the same rebuild path, as a reading of the code plus what I saw in the debugger, not as a bug report with a demo. TTMSFNCCustomGanttList keeps FDownNode (and FDragNode) across ClearNodes/ClearNodeList/BuildNodeList; only the focused node is reset there. In our application, OnDblClick of the chart opened a modal edit dialog, and closing it with OK rebuilt the list twice (BeginUpdate/EndUpdate). The mouse handling that runs after our event (HandleDblClick with the current mouse position, then MouseDown with ssDouble from TControl.WMLButtonDblClk) then worked with an FDownNode that, after each rebuild, pointed to a different, newly created node at the same address. With 1.3.3.14 the visible effect was that after Ctrl+A, a double-click on another task and OK, no task was selected any more. We now open our dialog after the double-click message has been handled (PostMessage), which solves it for us. Resetting FDownNode and FDragNode in ClearNodes/ClearNodeList, as is done for the focused node, would make this path safe.

Thank you for confirming the fixes and improvements worked as expected for you!

We'll look into your observation and apply it as an improvement for a future version.