I create windows form application using MVP design pattern. Example:
IViewInterface view = new FormSome();
IPresenter presenter = new Presenter(view);
In presenter constructor I do something like this:
public Presenter( IViewInterface view ) {
this.view = view;
this.view.someEvens += myMethod;
}
Now my question:
What happens when I do something like this:
IViewInterface view;
{
view = new FormSome();
IPresenter presenter = new Presenter(view);
}
// if my presenter exists here?
I don’t want to call any method from presenter explicitly. I only want to my presenter handle view’s event. Does GC remove my presenter from memory, or GC knows that my presenter handle view events, so as long as view exists, my presenter also will exist?
edit
I tested it and it works. But I’m not sure if it works because GC don’t destroy my presenter yet, or GC is more intelligent then I thought.
The presenter will still remain “alive” as it is referenced through the event set here
If the view is collected by the GC, the presenter is also destroyed.
But please note that you do not have a reference to the presenter at the moment! You can not access it anymore after the code snipped you’ve posted.
EDIT
This, by the way, happened to be a problem for me when I thought I had destroyed all instances of a class, but they were still active because I had accidentially used them as event targets. I had TCP commands being handled even though they shouldn’t have been handled.
That’s why I can tell that what you’re asking is actually the case 🙂