Skip to content
Tcl Source Code View Ticket
Login

View Ticket

can't link a array element to a c value

Closed496111ffffffffffffffffffffffffffffffffffCreated 25 years agoUpdated 25 years ago
Descriptionplain2001-12-22 15:35:28
Hi Tcl-Developers,

i've got a problem with the tcl 
function 'Tcl_LinkVar'. 

If a link a global tcl value with a C integer 
value ... everything is ok.
If a link a tcl array element with a C integer 
value ... my program core dumps !!

GDB say: There must be a problem in 
function 'TclLookupVar'

#0  0x080b0edb in TclLookupVar (interp=0x80e6078, 
part1=0x80d1835 "iValue2(a)", part2=0x0, flags=513, 
    msg=0x80dbb21 "set", createPart1=1, createPart2=1, 
arrayPtrPtr=0xbfffec44)
    at ./../generic/tclVar.c:179
#1  0x080b1f48 in Tcl_SetVar2Ex (interp=0x80e6078, 
part1=0x80d1835 "iValue2(a)", part2=0x0, 
    newValuePtr=0x8129a20, flags=513) 
at ./../generic/tclVar.c:1205
#2  0x080b1e23 in Tcl_SetVar2 (interp=0x80e6078, 
part1=0x80d1835 "iValue2(a)", part2=0x0, 
    newValue=0xbfffecfc "0", flags=513) 
at ./../generic/tclVar.c:1082
#3  0x080b1dcd in Tcl_SetVar (interp=0x80e6078, 
varName=0x80d1835 "iValue2(a)", 
newValue=0xbfffecfc "0", 
    flags=513) at ./../generic/tclVar.c:1026
#4  0x08090f85 in Tcl_LinkVar (interp=0x80e6078, 
varName=0x80d1835 "iValue2(a)", addr=0x80e5578 "", 
    type=1) at ./../generic/tclLink.c:113
#5  0x0804dc3d in LinkTest ()
#6  0x0804f609 in TclInvokeStringCommand 
(clientData=0x8135ee8, interp=0x80e6078, objc=1, 
objv=0xbfffee88)
    at ./../generic/tclBasic.c:1752
#7  0x0809bb2f in EvalObjv (interp=0x80e6078, objc=1, 
objv=0xbfffee88, 
    command=0x8126b32 "LinkTest\n\n\nbutton .b1 -text 
\"Show C values\" -command \"ShowTest\"\nbutton .b2 -
text \"Increase C values\" -command 
\"IncrValues\"\n\npack .b1 .b2\n", length=137, flags=0)
    at ./../generic/tclParse.c:932
#8  0x0809c4b7 in Tcl_EvalEx (interp=0x80e6078, 
    script=0x8126988 "proc  IncrValues {} {\n   
global   iValue1 iValue2\n\n   incr iValue1\n   incr 
iValue2(a) 2\n\n   puts \"Incr"..., numBytes=563, 
flags=0) at ./../generic/tclParse.c:1393
#9  0x08090873 in Tcl_EvalFile (interp=0x80e6078, 
fileName=0xbffff30c "../../scripts/tcltktest.tcl")
    at ./../generic/tclIOUtil.c:323
#10 0x4005faf4 in Tk_MainEx () 
from /usr/lib/libtk8.3.so
#11 0x0804dd60 in main ()
#12 0x401067ee in __libc_start_main () 
from /lib/libc.so.6


In calltree #2 the function Tcl_SetVar2 will be called 
in all cases (single
value or array element) with the same function call 
from Tcl_SetVar:


char *
Tcl_SetVar(interp, varName, newValue, flags)
    Tcl_Interp *interp;/* Command interpreter 
in which varName is
 * to be looked up. */
    char *varName;/* Name of a variable 
in interp. */
    char *newValue;/* New value for 
varName. */
    int flags;/* Various flags that 
tell how to set value:
 * any of 
TCL_GLOBAL_ONLY,
 * TCL_NAMESPACE_ONLY, 
TCL_APPEND_VALUE,
 * TCL_LIST_ELEMENT, 
TCL_LEAVE_ERR_MSG. */
{
    return Tcl_SetVar2(interp, varName, (char *) NULL, 
newValue, flags);
}


The 3rd. argument is in all cases (char *)NULL !! I 
think it must be the 
name or index-string of the array element ?

I use Tcl version 8.3.3 and Suse Linux 7.3. I found 
this problem also on 
a WindowsNT system with Tcl 8.3.4.

But I use a self-compiled Tcl/Tk system on IRIX 6.5 
without this problem. 
All functions and tcl scripts works fine (can't say 
why). I have to port
my program to WindowsNT and Linux. 

I think you can rebuild this behaviour if you change 
the tcl tests in
file './generic/tclTest.c' (line # 1891) from

if (Tcl_LinkVar(interp, "int", (char *) 
&intVar,
TCL_LINK_INT | flag) != TCL_OK) {
    return TCL_ERROR;
}

to

if (Tcl_LinkVar(interp, "int(value1)", (char 
*) &intVar,
TCL_LINK_INT | flag) != TCL_OK) {
    return TCL_ERROR;
}


I can't change my tcl scripts (e.g. use only single 
global values) because
there are a lot of linked values. If i do this, i have 
do add in all proc's
code line like this:

   global   value1 value2 value3 ...

Now where is only one line necesseary:

   global   array

The other reason is: I can't say how many values will 
be linked at runtime.
It depend on the user input.

Can I patch my tcl version or is there a bug free 
version available ?


Thank you

Carsten.



Here comes my C and tcl source (only a demo to narrow 
down the bug):

_____ BEGIN of C source ________________________

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int      iValue1, iValue2;


int  LinkTest (ClientData clientdata, Tcl_Interp 
*interp, int argc, char **argv)
{
   if (argc != 1) {
      Tcl_SetResult(
         interp,
         "wrong # args: should be \"LinkTest\"",
         TCL_STATIC);

      return TCL_ERROR;
   }

   printf("LinkTest\n");

   /* next line is ok */
   Tcl_LinkVar(interp, "iValue1",    (char *)&
(iValue1), TCL_LINK_INT);

   /* here is the problem !! */
   Tcl_LinkVar(interp, "iValue2(a)", (char *)&
(iValue2), TCL_LINK_INT);

   return TCL_OK;
}


int  ShowTest (ClientData clientdata, Tcl_Interp 
*interp, int argc, char **argv)
{
   if (argc != 1) {
      Tcl_SetResult(
         interp,
         "wrong # args: should be \"ShowTest\"",
         TCL_STATIC);

      return TCL_ERROR;
   }

   printf("ShowTest\n");
   printf("C iValue1 = %i\n", iValue1);
   printf("C iValue2 = %i\n", iValue2);
   printf("\n");

   return TCL_OK;
}


int  TclTkTest_Init (Tcl_Interp *interp)
{
   /* 
=======================================================
===================
    * Initialize Tcl and Tk
    */
   
   if (Tcl_Init(interp) == TCL_ERROR)        return 
TCL_ERROR;
   if (Tk_Init(interp) == TCL_ERROR)         return 
TCL_ERROR;


   /* 
=======================================================
===================
    * create new tcl commands
    */
   
   Tcl_CreateCommand(interp, "LinkTest", LinkTest, 
NULL, NULL);
   Tcl_CreateCommand(interp, "ShowTest", ShowTest, 
NULL, NULL);


   return TCL_OK;
}


int  main (int argc, char **argv)
{
   Tk_Main(argc, argv, TclTkTest_Init);

   return 0;
}

___ END of C source / BEGIN of tcl source _____


proc  IncrValues {} {
   global   iValue1 iValue2

   incr iValue1
   incr iValue2(a) 2
   puts "IncrValues:"
   puts "iValue1    = $iValue1"
   puts "iValue2(a) = $iValue2(a)"
   puts ""
}

# link values
LinkTest

# init values
set iValue1    0
set iValue2(a) 0


button .b1 -text "Show C values" -command "ShowTest"
button .b2 -text "Increase C values" -
command "IncrValues"

pack .b1 .b2

______ END tcl source ____________________________
hobbsplain2001-12-28 01:22:53
Logged In: YES 
user_id=72656

The problem, and the reason it works with one 
compiler/system and not others, I suspect is in the 
definition of the varName in the docs:

char *varName (in)
  Name of global variable. Must be in writable memory: Tcl 
may make temporary modifications to it while parsing the 
variable name. 

Note "Must be in writable memory".  Tcl will actually do 
the modifications when the variable is array-like (to 
separate the array name and key name).  Make sure that the 
name passed in is in writable memory and try again.
Log in (anonymous is fine) to add a comment.