Add Handler w/o Remove Handler

If I use Add Handler is it necessary to use Remove Handler? Does it leak memory if I do not use Remove Handler ?

[quote]http://documentation.xojo.com/index.php/AddHandler
A handler can handle any number of events, but an event can only have one event handler. To change the event handler for an event (after you have added one), you must first remove the existing handler using RemoveHandler and then add the new one.[/quote]

I personally don’t think it leaks memory if you add a handler and do not remove it…

I personally think that leaving a pointer to method lying around without reason is bad. Hence I always RemoveHandled when it’S not needed anymore.
A different case can be an handler added with the WeakAddressOf, which IMHO doesn’t need to be removed.

It’s a good habit to get into; cleaning up after yourself. Who knows one day, you may be spending a lot of time tracking down weird issues or crashes only to find it’s because you didn’t remove the handler when you needed to.

My habit in classes is to store the object to a property and AddHandler in the Constructor. In Destructor, I check to see if the property is nil. If not, I RemoveHandler and make it nil. Thus the assumption is that if the object exists, the handler for it exists as well.

I thought (correct me if I’m wrong) that Remove Handler requires you to know the method you’re removing and if you call Add Handler before you call Remove Handler, you get an exception.

These seem like two fundamental flaws to the system. Perhaps a replace handler method that takes care of these issues is needed?

Also it would be helpful to have a handler that can handle any event and is passed a dictionary of all the event parameters.

I would expect the remove handler is done by destructor anyway.

I believe that’s true in most cases. I seem to remember some rumblings that it didn’t always work that way in web edition. It’s been a few year so maybe that’s been cleared up.

Definitely call remove handler

I use AddHandler for socket event definitions, so I never use RemoveHandler, but should I be doing it when the app quits, or does the Xojo framework take care of that for me?

Good practice would suggest you should always call remove handler for every add handler.
You can cause cycles with address of and add handler if you’re not careful - and then you can leak as your app is running and eventually it just runs out of memory & dies and you have no idea why.

probably cause i suck at coding :stuck_out_tongue:

Welcome to the club… :slight_smile: