[CreateNew] and memory leak

Topics: CAB & Smart Client Software Factory
Jan 15, 2007 at 2:27 PM
Hi,

I am using the CreateNew attribute to build up controllers for several views. I instantiate such a view in the following way:

TView view = Items.AddNew<TView>();

Even though, I do clean up the view when it is no more needful:

Items.Remove(view);

the controller instantiated with the CreateNew attribute remains in the 'Items' collection. It is easy to reproduce with the following code:

TView view = Items.AddNew<TView>();
Items.Remove(view);

Suppose, the TView has one controller dependency injected with the use of 'CreateNew', when the above two lines of code are executed, the number of items in the collection is increased by one, while it should remain on the original level.

When the application gets complicated it might be a serious issue. If there are any references to the view, GDI objects are not released even if the view is disposed with the exlicit call of the Dispose() method.

Any information about the problem would be helpful and appreciated.

Thanks in advance.

AdamS
Jan 16, 2007 at 6:35 AM
You should remove the Controller along with the view.
In the SCSF ,the view call its presenter's dispose() when itself disposes .and then the presenter removed itself from the Workitem.Items in its dispose method.

Your controller could be act the same as the presenter of SCSF.
Jan 20, 2007 at 9:28 AM
If you have view with the presenter
in disposing the view you must dispose the presenter also

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (_presenter != null)
_presenter.Dispose();

if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}