From: Sharma Anshoo-A. <an...@mo...> - 2003-07-02 04:53:00
|
Don, I should have mentioned this in my first mail itself, we are using Tcl_LinkVar for linking the C variables to TCL. The code looks something like what is given below, #define MAX_MSG_LEN 0 app_init_proc() /* The Callback function passed in Tcl_Main */ { /* Some initialization... */ p_tcl_c_str = Tcl_Alloc(MAX_MSG_LEN); /* p_tcl_c_str being declared on global scope as (char *) */ Tcl_LinkVar(p_interp, "p_tcl_c_str", (char *)&p_tcl_c_str, TCL_LINK_STRING); /* Some more intialization... */ } /* Later at some point in the TCL Script */ set p_tcl_c_str "Hello World" /* The above statement releases the Memory allocated to p_tcl_c_str in app_init_proc. And reallocates it just enough memory to hold the given string. */ /* At still later point in C Code */ memset(p_tcl_c_str, 0, MAX_MSG_LEN); /* Results in a crash since the size of the string is no longer MAX_MSG_LEN now. Any thoughts on how to circumvent this problem? We have been able to solve this by reallocating the memory in C code everytime we want to assign the string. Something like, p_tcl_c_str = Tcl_Realloc(p_tcl_c_str, MAX_MSG_LEN); Is a more elegant solution available? Best Regards Anshoo. -----Original Message----- From: Donald G Porter [mailto:dg...@em...] Sent: Wednesday, July 02, 2003 1:27 AM To: je...@Ac...; tcl...@li...; Sharma Anshoo-A19642 Cc: Karpoor Lakshmi Sanjay-A17738 Subject: Re: [Tclplugin-core] TCl problem > During the Initialization we request TCL to allocate a pointer > "p_tcl_c_str" (say 1024 bytes) using the Tcl_Alloc call. When we later write to this variable from a TCL Script, TCL reallocates this variable memory which is equal to length of the string that is being written. For Example if we do a "set p_tcl_c_str Hello_World", It will reallocate this variable only 12 bytes and release the previous chunk. As, in the C code we are not aware of this change we write to the C variable expecting that it would be able to support the write upto 1024 bytes and hence the process crashes. It sounds like you are attempting to program based on a mistaken assumption that a C variable of a particular name is somehow tied to a Tcl variable (a Tcl_Var) of that same name. No such automatic connection exists. If you need access to the same variable at both Tcl and C levels, you should be working with Tcl_Var's all the time and use the procedures like Tcl_SetVar() and Tcl_GetVar() to read and write them. Another option that might be suitable for certain situations is to create a "link" between a C variable and a Tcl variable using the Tcl_LinkVar() family of routines. If these pointers do not answer your problem, then try again with some specific code examples of what you are attempting to do. | Don Porter Mathematical and Computational Sciences Division | | don...@ni... Information Technology Laboratory | | http://math.nist.gov/~DPorter/ NIST | |______________________________________________________________________| |