Application idle calling action objects after free lazarus / 21-General ---------------------------------------------------------------------- mewpokemon · 2022-02-26 21:15 Heyas! I have actually solved this problem I believe however I could only find a single vague reference to issues related to it so I thought I would post this here to assist anyone else affected. I am creating frames which have an action list with actions and I am "transplanting" controls using those actions onto other forms in the application. The problem comes when reclaiming the controls and freeing the frames.  It seems that on some odd occasion (it only happens every so often) that the application hasn't been informed that the said actions have been freed in time before the on idle update cycle begins and it attempts to access now dangling pointers.  This is despite every attempt to hide and remove the controls. I have found (I believe) that the problem is in the TList<> type...  It is unsuitable for storing TAction components because notifications aren't being sent about the objects correctly.  I found another reference to TList vs TObjectList having these update notification issues and switching to the use of TObjectList<> seems to have resolved the problem. I will post if I find any further information. ---------------------------------------------------------------------- mewpokemon · 2022-02-26 22:00 Actually, I still seem to be having the problem. ---------------------------------------------------------------------- howardpc · 2022-02-26 22:03 Can you post a cut-down compilable example that shows the problem you are having? ---------------------------------------------------------------------- mewpokemon · 2022-02-26 22:13 I'll try now since I'm still having the trouble.  It will take some time. ---------------------------------------------------------------------- mewpokemon · 2022-02-27 01:26 Hmm.  I can't reproduce the problem in a sample application. However, I have simplified my own code down to the process of pressing two buttons (actions) that invoke simple construction and destruction routines.  The problem is sporadic so I really think its not my code as such? To produce the problem, repeatedly press the last two speed buttons on the "tool bar" presented at program launch, first the second from the end and then the last one.  Alternately press the two buttons until you get an exception.  They are shown in the screen shot. Please let me know if there are any issues with compilation and so on.  I'm working on it in Linux atm. I'm using Lazarus 2.2.0 and FPC 3.2.2. ---------------------------------------------------------------------- mewpokemon · 2022-02-27 02:02 Oh!  To see how it is the action causing the dilemma, simple remove it from SpeedButton14 of TD64ExplorerManageFrame in FrameD64ExplorerManage unit. Its difficult to trace and certain combinations show how it is OnIdleActions (or similarly named routine) better than others. ---------------------------------------------------------------------- howardpc · 2022-02-27 20:56 I think you have an issue with re-entrant code. In my limited testing the following modifications to your unit  DModeD64ExploreMain does away with the problem - simply add this method: 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 TD64ExplorerMainDMod.UnprepareDelete(Data: PtrInt);var  value: SizeInt absolute Data;begin  FTaskFrames[value].Unprepare;  FTaskFrames.Delete(value);end; and amend your existing ActionTest2Execute method to the following: 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 TD64ExplorerMainDMod.ActionTest2Execute(Sender: TObject);var  hi: PtrInt;begin  hi := FTaskFrames.Count-1;  if hi > -1 then    Application.QueueAsyncCall(UnprepareDelete, hi);end; ---------------------------------------------------------------------- mewpokemon · 2022-03-02 07:06 Hi! Thank you, I think I have fixed the issue based on your suggestion.  I am now using the FFreeTasks list and calling Unprepare in ProcessFreeFrames (executed synchronously by a thread). I had suspected something like that was required but I didn't try putting the Unprepare call there. ---------------------------------------------------------------------- mewpokemon · 2022-03-03 17:18 Actually, it doesn't completely solve the problem. I'm still getting the same error/bug when closing the application with any frames open.  Unprepare is not being called in this case and its happening before the data module OnDestroy hander. I've attached the chargen file since it was missing. ---------------------------------------------------------------------- mewpokemon · 2022-03-03 17:42 I have put a call to Application.DisableIdleHandler in the OnCloseQuery event and return Done = False in an OnIdle event handler when the application is terminated but it seems both of these are too late to solve the problem. ---------------------------------------------------------------------- howardpc · 2022-03-03 19:54 Maybe you could try adding an OnBeforeDestruction handler for your datamodule and see if you are able to use that to clean up open frames? It is a weakness that the VCL (and subsequent LCL clone of the VCL pattern) design is based on a single-threaded application event loop. When you have several layers of components with complex mutual interactions it can become difficult to synchronise events that must fall in a particular sequence to be executed correctly. ---------------------------------------------------------------------- mewpokemon · 2022-03-04 00:30 I have an OnDestroy handler for the DataModule, there is no OnBeforeDestruction. The OnDestroy handler is not reached in time, before the problem arises and it makes no difference which component I use as the owner in the constructor for the frames.  I am quite convinced I am triggering some erroneous behaviour in the runtime but I can't reproduce it as as simple example... Like I say, even OnCloseQuery of the application main form is too late to make any difference it seems, where calling DisableIdleHandler has no effect.