Jump to content
Tom F

Freeing a non-modal form

Recommended Posts

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

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.

  • Like 1

Share this post


Link to post
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.

 

  • Like 2

Share this post


Link to post
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.

  • Like 2

Share this post


Link to post

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.

  • Like 1
  • Thanks 1

Share this post


Link to post
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
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 by Virgo

Share this post


Link to post

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 by David Schwartz

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×