Avoid stale handles
- Last Updated: October 5, 2023
- 2 minute read
- OpenEdge
- Version 13.0
- Documentation
A stale handle is one that holds a pointer to an object that no longer exists. If you make sure that your objects always live just as long as they should, then you should not get this type of error. But it is still very good practice to check the validity of a handle when there is any chance that it might be invalid, which almost certainly includes whenever it comes from another procedure that might be maintained independently of the one that needs to use the handle.
You can use the VALID-HANDLE function to check whether a handle is
valid. If you do use this check, then always determine how your procedure should react
if a handle that ought to be valid turns out not to be. Simply substituting your own
“Invalid handle!” message for the default one that ABL gives you is
not the right approach. There are many things you can try to blame the end user of your
application for, but supplying a procedure with an invalid object handle is not one of
them.
Remember, too, that once you have encountered an invalid handle, whether it is a stale handle for a deleted object or simply a handle variable that was never properly initialized, your end user will get another ugly message for every statement in your procedure that vainly tries to reference it. In the Runmake.p example, the procedure generated two internal system errors because the buffer handle was not valid. It could easily have been a hundred errors, if the procedure had gone on to continue to try to work with that invalid handle. Never risk subjecting your end users to this abuse.
VALID-HANDLE function is also very useful and necessary anytime your
code is walking through a chain of handles, such as in this procedure that displays all
the windows currently running in the session:
|
You could also use the VALID-HANDLE function as a signal that tells your
procedure whether an attempt to locate a particular object was successful or not,
somewhat like the AVAILABLE function after a FIND
statement on a buffer.