On Tue, 1 Jul 2003, Alexander Leykekh wrote:

> ... and another one - why does the following snippet of code fail? What is
> the proper way to swap values of 2 Tcl variables holding domNodes?

If you specify [$doc createElement foo n1] then when n1 is changed or
unset (i.e., if within a proc that goes out of scope), then the node is
automagically freed for you.

Setting n2 isn't the problem, but when you do [$doc createElement bar n1],
then the value of n1 has changed, and the element that was initially
created (domNode0x249088) gets freed, and so, Tcl evaluates $ns to
domNode0x249088, but then that command no longer exists.

> server1:nscp 218> dom createDocument root doc
> domDoc0x248e90
> server1:nscp 219> $doc createElement foo n1
> domNode0x249088
> server1:nscp 220> set n2 $n1
> domNode0x249088
> server1:nscp 221> $doc createElement bar n1
> domNode0x2490c0
> server1:nscp 222> $n2 nodeName
> invalid command name "domNode0x249088"

Two ways to rewrite the above:

dom createDocument root doc
$doc createElement foo n2
$doc createElement bar n1
$n2 nodeName

or, more in line with what you wrote originally:

dom createDocument root doc
set n1 [$doc createElement foo]
set n2 $n1
set n1 [$doc createElement bar]
$n2 nodeName

With the latter you are responsible for deleting (freeing) the element
when (if) you no longer need it.

Michael


--
AOLserver - http://www.aolserver.com/

To Remove yourself from this list, simply send an email to <[EMAIL PROTECTED]> with the
body of "SIGNOFF AOLSERVER" in the email message. You can leave the Subject: field of 
your email blank.

Reply via email to