Tcl objects usually remain one type over their life, but occasionally a
Tcl object must be converted from one type to another. For example, a
Java object handle may be passed as the argument to the llength
command. The internal rep of the Tcl object is converted from a Java
object handle to a Tcl List. When this happens the ref count of the Java
object handle is decremented. If the ref count becomes zero, the Java
object handle is invalidated and the Tcl variable no longer accesses a
Java object. For example:
The solution is to guarantee that the Java object handle's ref count
does not become zero. Use the nsjava::lock and nsjava::unlock
commands to maintain a permanent reference to the Java object handle.
For example:
# Create a new Java Object. The ref count equals one.
set o [nsjava::new java.lang.Object]
# Call a method of the Java Object.
puts [$o toString]
# Convert the Java object handle to a Tcl List. This
# decrements the ref count by one. The ref count equals
# zero and the Java object is invalidated.
llength $o
# This command will generate an error, because the
# Tcl object no longer references a valid Java object.
puts [$o toString]
# Create a new Java object. The ref count equals one.
set o [nsjava::new java.lang.Object]
# Lock the Java Object handle so it is not invalidated.
# The ref count now equals two.
nsjava::lock $o
# Convert the Java object to a Tcl List. This decrements
# the ref count by one. The ref count equals one and the
# Java object remains valid.
llength $o
# Now this command works. It also increments the ref count
# of the java object, because a Tcl List is being converted
# to the original Java object handle.
puts [$o toString]
# Remove the lock and decrement the ref count.
nsjava::unlock $o
# Now this will fail as in the previous example.
llength $o
puts [$o toString]
Copyright © 1997-1998 Sun Microsystems, Inc.