Page 1 of 1

Odd behavior on deleting task.

Posted: Wed Dec 25, 2019 3:49 pm
by rbaroniunas
In my application i have a TListView that list all of the scheduled tasks for the user. When i attempted to delete a task based on a button event i received a access violation (application did not crap out) on an invalid pointer.

The only thing i did was move the lyTasks.Item[Index].Delete from after the DeleteTask Method and the AV went away.

Original (AV on this one).
Task.Activate;
tskScheduler.DeleteTask(Task.Name);
lvTasks.Items[Index].Delete;

New (this works)
lvTasks.Items[Index].Delete;
Task.Activate;
tskScheduler.DeleteTask(Task.Name);

I thought it was very odd by simply moving the ListView delete it works. If i do not have the ListView Delete method, the item remains in the listview.

Is there anything i am doing incorrectly ?

Posted: Thu Dec 26, 2019 4:18 am
by isiticov
This is strange. As an idea: may be in your code you do some "background" task like drawing or checking/changing the state icon of list item depending on assigned task or so on? So when the task deleted this causes AV error.

Posted: Sat Dec 28, 2019 4:22 pm
by rbaroniunas
Not that i see. The funny thing is when i move the delete method prior to the taskscheduler doing it's delete, the piece works. What exactly is the taskscheduler doing when it deletes a task ?

Posted: Sat Dec 28, 2019 7:20 pm
by rbaroniunas
Here is the entire routine

for Index := lvTasks.Items.Count - 1 downto 0 do
if lvTasks.Items[Index].Selected then
begin
Task := TTaskItem(lvTasks.Items[Index].Data);
try
lvTasks.Items[Index].Delete;
Task.Activate;
tskScheduler.DeleteTask(Task.Name);
lvTasks.Refresh;
except
raise EUnexpectedError.Create('TScheduledTasks.sBtnDeleteClick');
end;
end;

Posted: Sun Dec 29, 2019 2:49 am
by isiticov
It doesn't do anything "special". Just deletes the TTaskItem from internal list and then call's Task Service's method to delete the actual task.

P.S. I'm sorry but are you sure that calling lvTasks.Referesh inside a loop is correct?