Tom F 85 Posted March 3, 2023 How can a non-modal VCL sub-form notify its owner that the user has closed it so the owner can free it? If the non-modal form's OnClose event calls its owner, the owner can't free the form at that time because the non-modal form's OnClose event is still being run. Share this post Link to post
aehimself 399 Posted March 3, 2023 You can use WIndows messages via PostMessage to notify the owner. With that said, if a third component is controlling the lifecycle of said form it's better to review your program design. E.g. have a class which is showing / hiding forms, and create your forms with (nil) as owner. 1 Share this post Link to post
FPiette 385 Posted March 3, 2023 38 minutes ago, Tom F said: If the non-modal form's OnClose event calls its owner, the owner can't free the form at that time because the non-modal form's OnClose event is still being run. You have to use Release instead of Free or Destroy. Release will defer the actual Free until after all events handlers are done. 2 Share this post Link to post
FPiette 385 Posted March 3, 2023 3 minutes ago, aehimself said: You can use WIndows messages via PostMessage to notify the owner. That's what the Release method does. Much easier with Release. 2 Share this post Link to post
Virgo 18 Posted March 3, 2023 ChildForm sest Action to caFree on OnClose event handler Owner calls FreeNotification(ChildForm) after creating the childform. Owner overrides Notification() and when it receives notification, that childform is freed, then sets variable to nil (or whatever needs to be done). Owner calls RemoveFreeNotification(ChildForm) when it is going to manually free childform or when owner itself is going to be freed. 1 1 Share this post Link to post
Remy Lebeau 1436 Posted March 3, 2023 9 hours ago, Virgo said: Owner calls FreeNotification(ChildForm) after creating the childform. Owner calls RemoveFreeNotification(ChildForm) when it is going to manually free childform or when owner itself is going to be freed. If the Owner form assigns itself as the actual Owner (from the RTL's perspective) of the Child form, then the RTL will handle all of that for you automatically. All you would have to do in your own code is have the Owner form override the Notification() method. However, personally, I would just have the Owner form assign its own handlers to the Child form's OnClose and OnDestroy events instead. That way, the Owner, not the Child, decides whether or not to set Action=caFree in the OnClose event, and the Owner can still react to the Child being destroyed without having to override the Notification() method. Share this post Link to post
Virgo 18 Posted March 3, 2023 (edited) 2 hours ago, Remy Lebeau said: If the Owner form assigns itself as the actual Owner (from the RTL's perspective) of the Child form, then the RTL will handle all of that for you automatically. All you would have to do in your own code is have the Owner form override the Notification() method. However, personally, I would just have the Owner form assign its own handlers to the Child form's OnClose and OnDestroy events instead. That way, the Owner, not the Child, decides whether or not to set Action=caFree in the OnClose event, and the Owner can still react to the Child being destroyed without having to override the Notification() method. Right... Actually with owner set only overriding notification is needed... And assigning OnClose and OnDestroy from owner form is probably simpler idea.. with assigned OnDestroy clearing handles instead Notification. I mean we actually use OnDestroy to clear global form variables in form units, when form is free (with check, if Self is same as variable). Edited March 3, 2023 by Virgo Share this post Link to post
David Schwartz 430 Posted March 5, 2023 (edited) Why would you create a bunch of non-modal forms (or other resources) and not keep references to them on a list of some kind? You're just begging for what amounts to memory leaks when those forms get hidden and forgotten. I worked on a system once that let people sign-on and they could open non-modal windows and other resources like DB connections, while doing their work. Everything they created was put on a list. When they signed-out (or a timer triggered it automatically), all of their resources got deleted. I've seen very few situations where non-modal windows got created and stuck around for very long; but when there was a chance of longer-term persistence, there would always be a list that kept track of them. Edited March 5, 2023 by David Schwartz Share this post Link to post