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 java::lock and java::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 [java::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 [java::new java.lang.Object]
# Lock the Java Object handle so it is not invalidated.
# The ref count now equals two.
java::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.
java::unlock $o
# Now this will fail as in the previous example.
llength $o
puts [$o toString]
If you want to interactively enter commands without worrying about the details of Java object reference counting, you can use the java::autolock command.
Copyright © 1997-1998 Sun Microsystems, Inc.