MojoDK
MojoDK

Reputation: 4528

Monotouch: Do I have to dispose view?

In my AppDelegate, I have a static event called LoggedOn.

My parent viewcontroller pushes a client viewcontroller.My client viewcontroller adds a delegate to the AppDelegate.LoggedOn event.

When I pop the client viewcontroller, the listner for the LoggedOn event is still listening .. emmmmm ... do I need to like dispose it or something?

I thought the whole client view was disposed when I pop'ed it?

Thanks! Mojo

Upvotes: 1

Views: 822

Answers (1)

Anuj
Anuj

Reputation: 3134

It's best to dispose of EventHandlers to global events like the LoggedOn event you have. These are called strong references and would prevent the ViewController that contains the delegate from being garbage collected.

I would do something like this in the ClientViewController:

public override void Dispose(bool disposing) 
{
    base.Dispose(disposing);

    if(disposing) {
        AppDelegate.LoggedOn -= Handle_LoggedOn;
    }
}

Upvotes: 1

Related Questions