You could keep a list of valid gadget handles in a bank (for example) and compare the values to the value set in a script when the script runs, unless you have tens of thousands of gadgets the speed impact will be negligible although for a gui app it most definitely will not be noticeable.
EDIT :
kind of like this:
when a new gadget is created add its handle to a type list , and when it is removed/deleted/freed remove the handle from the list.
This list can be used to compare any script references to gadget handles with.
EDIT :
kind of like this:
when a new gadget is created add its handle to a type list , and when it is removed/deleted/freed remove the handle from the list.
This list can be used to compare any script references to gadget handles with.