update
command in Itcl
Calling the Tcl update
command,
tkwait
, or even
update idletasks
within a method of any Itcl
object is very dangerous. The Itcl object may be deleted during the
update, so when the update returns, you will be trying to execute a
method for an object that no longer exists.
You must not call update in a method.
At least in version 2.0 of Itcl, this can cause a core dump.
There are two alternatives: safeUpdate
and safeEval
.
safeUpdate
procedure
Tycho provides a procedure called
::tycho::safeUpdate
that takes a single argument, the name of an Itcl object.
It calls update
. If the object is deleted during
the call to update
, then a
silent error is triggered.
This means that safeUpdate
will never return,
so execution does not continue within the calling thread.
A typical call within an Itcl method looks like this:
::tycho::safeUpdate $this
A "silent error" is an ordinary error where no error message is
reported to the user, like a throw
and catch
in some languages.
You can directly invoke a silent error with the following command:
::tycho::silentError
safeEval
method
The Tycho base class
TArchetype
has a protected method called
safeEval
.
This method simply disables destruction of the object,
then evaluates its arguments, then re-enables destruction of the object.
It returns the result of evaluating the arguments.
Thus, if during evaluation of the arguments an attempt is made to destroy
the object, the attempt will be intercepted and deferred until after
evaluation of the arguments is complete.
The safeEval
method should be used to invoke all modal
dialogs, as in the following example:
safeEval ::tycho::warn "this is a warning"