Garbage collection and dialog boxes

Posted by Laura Stern on 05-Aug-2009 15:15

It has come to our attention that a form object used as a dialog box (i.e., one that you call ShowDialog() on) is not garbage collected.  Therefore none of the controls on the form are garbage collected and the destructor of the class will not run.  This is actually not a bug in the PVM.  It is a side effect of a .NET "feature".  Here is a statement from Microsoft on how dialog boxes work when you close them:

"Unlike modeless forms, the Close method is not called by the .NET Framework when the user clicks the close form button of a dialog box or sets the value of the DialogResult property. Instead the form is hidden and can be shown again without creating a new instance of the dialog box. Because a form displayed as a dialog box is not closed, you must call the Dispose method of the form when the form is no longer needed by your application."

The prescribed model for implementing .NET forms in the ABL is to encapsulate the form as an ABL extended .NET object (i.e., an ABL class that inherits from a .NET form).  The PVM cannot garbage collect any ABL extended .NET object until not only all ABL references are gone, but also until any .NET references are gone.  Though in the case of forms, the PVM will garbage collect a form if Dispose has been called on it AND there are no more references to it in the ABL.  However, based on the Microsoft statement above, neither of these conditions will ever happen by default.  The .NET Framework does not call Dispose on the form automatically when it is closed, as it does with non-modal forms.  In addition, the form is not garbage collected by .NET.  Therefore, the PVM does not delete the ABL part of the class either.

The same problem exists in native .NET.  You can easily find several articles out on the web about it.  Because of this, we are recommending that the application either call Dispose() or use DELETE OBJECT on a form used as a dialog box when it no longer needs it.

Laura

All Replies

Posted by Thomas Mercer-Hursh on 05-Aug-2009 15:32

Yet another example of the virtues of taking out one's own garbage ...

Posted by Admin on 06-Aug-2009 07:35

tamhas schrieb:

Yet another example of the virtues of taking out one's own garbage ...

While I fully agree with you, I find it very impressive to know, that .NET Controls perform in the AVM as they do in any other .NET environment. In good and bad.

I take that as a positive test result, while I can imagine that it might have taken a while to find the (simple) solution.

But I hope, that this comment from Laura will be added to the product documentation, probably to the WAIT-FOR statement as this is where .NET Dialog Forms are invoked.

Posted by Laura Stern on 06-Aug-2009 09:05

Thanks for your comments.  Yes, this will most certainly be added to the documentation.

This thread is closed