Re: TCL Forces : OUT OF MEMORY

From: BIN ZHANG (zhngbn_at_gmail.com)
Date: Fri Sep 12 2008 - 17:43:06 CDT

Hi, All
   I just checked the function "vecscale" in NAMD source file:
"TclCommands.C" and found something suspicious.
It seems to me that the variables (scalar and vector) defined on line
171 are not freed.
   Will this be problem?
   But when I try to modify it as in the code (Bin Edit), then the code
just failed running.
   Any suggestions?
   Thanks a lot.
Bin

/* ====================================== */

     134 // Function: vecscale
     135 // Returns: scalar * vector or vector * scalar
     136 // speedup is 1228/225 = 5.5 fold
     137 int proc_vecscale(ClientData, Tcl_Interp *interp, int argc,
     138 char *argv[])
     139 {
     140 if (argc == 1) {
     141 Tcl_SetResult(interp,"no value given for parameter \"c\"
to \"vecscale\"",TCL_VOLATILE);
     142 return TCL_ERROR;
     143 }
     144
     145 if (argc == 2) {
     146 Tcl_SetResult(interp,"no value given for parameter \"v\"
to \"vecscale\"",TCL_VOLATILE);
     147 return TCL_ERROR;
     148 }
     149 if (argc != 3) {
     150 Tcl_SetResult(interp,"called \"vecscale\" with too many
arguments",TCL_VOLATILE);
     151 return TCL_ERROR;
     152 }
     153
     154 int num1, num2;
     155 char **data1, **data2;
     156 if (Tcl_SplitList(interp, argv[1], &num1, &data1) != TCL_OK)
{
     157 return TCL_ERROR;
     158 }
     159 if (Tcl_SplitList(interp, argv[2], &num2, &data2) != TCL_OK)
{
     160 Tcl_Free((char*) data1);
     161 return TCL_ERROR;
     162 }
     163 int result = TCL_OK;
     164 if (num1 == 0 || num2 == 0) {
     165 result = TCL_ERROR;
     166 Tcl_SetResult(interp,"vecscale: parameters must have
data",TCL_VOLATILE);
     167 } else if (num1 != 1 && num2 != 1) {
     168 result = TCL_ERROR;
     169 Tcl_SetResult(interp,"vecscale: one parameter must be a
scalar value",TCL_VOLATILE);
     170 } else {

/* ========= suspecious =========== */
     171 char *scalar, **vector;

     172 int num;
     173 if (num1 == 1) {
     174 scalar = data1[0];
     175 vector = data2;
     176 num = num2;
     177 } else {
     178 scalar = data2[0];
     179 vector = data1;
     180 num = num1;
     181 }
     182 char s[TCL_DOUBLE_SPACE];
     183 double val1, val2;
     184 if (Tcl_GetDouble(interp, scalar, &val1) != TCL_OK) {
     185 result = TCL_ERROR;
     186 } else {
     187 for (int i=0; i<num; i++) {
     188 if (Tcl_GetDouble(interp, vector[i], &val2) != TCL_OK)
{
     189 Tcl_SetResult(interp,"vecscale: vector contains a
non-number",TCL_VOLATILE);
     190 result = TCL_ERROR;
     191 break;
     192 }
     193 Tcl_PrintDouble(interp, val1 * val2, s);
     194 Tcl_AppendElement(interp, s);
     195 }
     196 }
/* ============================ */
     197 // Bin Edit
     198 Tcl_Free((char*) scalar);
     199 Tcl_Free((char*) vector);
     201 // Bin Edit
/* ============================ */
     202 }
     203 Tcl_Free((char*) data1);
     204 Tcl_Free((char*) data2);
     205 return result;
     206 }
On Sep 12, 2008, at 11:38 AM, BIN ZHANG wrote:

> Hi, All:
> I was writing some TCL force scripts to apply constraints to the
> system. In order to get the force, I need to do a lot of floating
> point math. And unfortunately, during the calculation, I got the
> 'OUT OF MEMORY" error.
> At first I thought this could be that the variables I defined
> during the procedure are not freed. So I tried to unset them
> explicitly, but still got the same error.
> The scripts are attached.
> Any suggestion is greatly appreciated.
> Thanks a lot.
> Bin
>
> <derv.tcl><wham_1d.tcl>

This archive was generated by hypermail 2.1.6 : Wed Feb 29 2012 - 15:49:50 CST