Concerning AddHandler he mentions to always use RemoveHandler after all your stuff is done. This you can also read in the documentation:
As a curious person I have two questions:
What exatly will happen if you don’t use RemoveHandler? Bob says in his video, that “bad things can happen” and there “might hang some memory leaks around”. What exactly are these bad things? And concerning memory leaks:
Can I use AddHandler with a local declaration? Meaning not to use a layout property:
Var dlg as new myDlg
' myDlg is a class representing a Dialog
AddHandler dlg.Close, AddressOf CardsSorted
dlg.show
In the method CardsSorted I am not able to reference dlg because of its local scope:
RemoveHandler dlg.Close, AddressOf CardsSorted
' This will fail because dlg is unknown
But: If I’m using dlg locally, doesn’t XOJO kill this instance automatically after I’m losing my scope to this instance? So I don’t have to be concerned about unwanted memory effects?
[quote=485660:@Frank Kersten]
But: If I’m using dlg locally, doesn’t XOJO kill this instance automatically after I’m losing my scope to this instance? So I don’t have to be concerned about unwanted memory effects?[/quote]
I have not watched the video, but…
Once you start using AddHandler etc… it becomes much harder for XOJO to guess what you are up to and clean up after you. So rather than messing up your program by guessing when you are finished, it leaves it up to you to let it know when it can clean up.
A handler can be as simple as a calculation or as complex as maybe using a third party plugin that allocates memory, runs independent threads, opens database connections. Just because your object goes out of scope, it does not mean everything in the handler did… you need to make sure it did.
The difference between an amateur and a professional programmer: an amateur tests until it works, a professional until it breaks. Just because your code works, does not mean it is doing what you intended it to do. Run it in the debugger to confirm it, check on your DBMS to make sure it closes connections, check the process for memory leaks and to ensure it closes down correctly etc.
Many bad blunders can be avoided when you stop making assumptions or at least allows you to close down gracefully rather than die a horrible death.
In your code, it is probably not a good idea to dynamically add a handler to the close event in any case.
with a memory leak your or other apps run out of memory after a while and can not continue.
your app is good if it runs permanent for months.
usually a object get deleted if it is not used somewhere.
if you add your variable to a global list then the object exists there after the method is finished.
the variable is only valid inside her scope.
AddHandler is seductive in its simplicity
But it is easy to end up with spaghetti code
Seriously try everything else before you resort to addhandler
Its great for the case where you want a thread to be self contained and have a timer that can run periodically to update your UI
After you step beyond this be REALLY careful
[quote=485670:@Markus Rauch]i can’t promise that is correct but it should look like.
i will only work if close is not used at the window itself.
a event came only once.[/quote]
Thanks Markus. That solved my question. I didn’t know that “sender” (as shown in the documentation) could be replaced with another name.