User control memory leak - FYI

Posted by Admin on 03-Feb-2009 08:04

I've reported an issue to support (bug# OE00180507) regarding user controls not being cleared from memory when the containing form is deleted.

If a user control is created and subsequently added to a form at design time, it would be assumed the user control would be deleted when the form is. I've found, by running through SESSION:FIRST-OBJECT / SessionObject:NEXT-SIBLING that after the form is deleted, the user controls stays in the list and is not delete from memory.

As a workaround, I've created a User Control base class that I now inherit for any User Control I create. In the base class, I subscribe to its Load event. I use the Load event method and a manually added FormClosing event (both shown below) to delete the User Control when the containing form is deleted.

So far this has worked well.

JL

METHOD PRIVATE VOID BaseUserControl_Load

( INPUT sender AS System.Object,

INPUT e AS System.EventArgs ):

DEFINE VARIABLE ControlParent AS Progress.Windows.Form NO-UNDO.

ControlParent = CAST(THIS-OBJECT:FindForm(),Progress.Windows.Form).

IF VALID-OBJECT(ControlParent) THEN

ControlParent:FormClosing:SUBSCRIBE(BaseParentFormClosing) NO-ERROR.

RETURN.

END METHOD. /* BaseUserControl_Load */

METHOD PRIVATE VOID BaseParentFormClosing

( INPUT sender AS System.Object,

INPUT e AS System.Windows.Forms.FormClosingEventArgs ):

DELETE OBJECT THIS-OBJECT NO-ERROR.

RETURN.

END METHOD. /* BaseParentFormClosing */

All Replies

Posted by rbf on 03-Feb-2009 09:02

I suppose Garbage Collection should take care of this sooner or later.

What is it that makes you worry that it doesn't? Did you experience crashes?

Posted by Admin on 03-Feb-2009 10:15

Garbage Collection wasn't cleaning this up. We could leave the app running for a fair amount of time and the user control never went away.

Basically I was investigating memory leaks; saw memory increase but never really come back down. I realize we would never see the memory reduce instantly, but it didn't seem to drop at all.

Also, in a couple of places, I saw other objects stay in the SESSION object list until I added the delete of the user control.

Posted by Thomas Mercer-Hursh on 03-Feb-2009 11:04

Seems to reinforce my belief that one should take out one's own garbage, even if one has garbage collection.

Posted by Roger Blanchard on 30-Mar-2009 09:04

Jim,

I am not sure if you have loaded SP1 yet but this appears to be resolved.

Posted by Admin on 01-Apr-2009 07:43

Roger,

Thanks for the heads up. I was out for a couple of days but my plan is to install SP1 today.

JL

Posted by Roger Blanchard on 01-Apr-2009 07:49

Hopefully you seee the same thing. The DESTRUCTOR in all of my inherited controls would not fire prior to SP1 and now each and everone of them fire.

Posted by Admin on 01-Apr-2009 07:52

Interesting...

Never even thought to check the destructor.

This thread is closed