Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 7e5bb31c397af0e3a703e550ae7feee97f1b16b0
Ticket: 218367ffffffffffffffffffffffffffffffffff
Command optimisation problem?
User & Date: nobody on 2000-10-26 00:56:46
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 4212 Bug
    Version: 8.2.3
    SubmitDate: '2000-02-22'
    LastModified: '2000-05-10'
    Severity: MED
    Status: Closed
    Submitter: techsupp
    ChangedBy: hobbs
    OS: All Windows
    OSVersion: WinNT
    Machine: Desktop, Pentium II, 128Mb RAM, heaps of disk space
    FixedDate: '2000-10-25'
    ClosedDate: '2000-05-10'
    
    
    Name: Andy
    
    ReproducibleScript:
    Compile the following C code using tcl8.2.3 and execute it...
    
    #include "tcl.h"
    #include "tclInt.h"
    #include "iostream"
    #include "string"
    #include "sstream"
    
    using namespace std;
    
    typedef int HANDLE_TYPE;
    
    static Tcl_Interp *interp = NULL;
    
    void sclPointUpdateStringProc(Tcl_Obj *objPtr);
    void sclPointFreeIntRepProc(Tcl_Obj *objPtr);
    void sclPointDupIntRepProc(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr);
    int sclPointSetFromAnyProc(Tcl_Interp *interp, Tcl_Obj *objPtr);
    
    Tcl_ObjType sclPointType = {
        "point",
        sclPointFreeIntRepProc,
        sclPointDupIntRepProc,
        sclPointUpdateStringProc,
        sclPointSetFromAnyProc
    };
    
    Tcl_Command createTclCommand(Tcl_Interp *interp, 
            const string &cmdName, Tcl_ObjCmdProc *proc, 
            ClientData clientData, Tcl_CmdDeleteProc *deleteProc) {
        return(Tcl_CreateObjCommand(interp, const_cast<char*>(cmdName.c_str()), 
            proc, clientData, deleteProc));
    }
    
    int sclPointObjHandler(ClientData clientData,
            Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]) {
      cout << "Called the object handler\n";
      return TCL_OK;
    }
    
    Tcl_Command createTclObjCommand(Tcl_Interp *interp, 
            const string &cmdName, Tcl_ObjCmdProc *proc) {
        return(Tcl_CreateObjCommand(interp, const_cast<char*>(cmdName.c_str()), 
                proc, (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL));
    }
    
    Tcl_Obj *newTclStringObj(const string &str) {
        return(::Tcl_NewStringObj(const_cast<char*>(str.c_str()), -1));
    }
    
    bool setTclVariable(Tcl_Interp *interp, 
        const string &variable_name, Tcl_Obj *variable_value) {
    
        Tcl_Obj *obj_name = NULL;
        if((obj_name = newTclStringObj(variable_name)) == NULL)
            return false;
        if(Tcl_ObjSetVar2(interp, obj_name, NULL, variable_value, 
                TCL_LEAVE_ERR_MSG) == NULL)
            return false;
        return true;
    }
    
    Tcl_Obj *sclNewPointObj(HANDLE_TYPE handle, Tcl_Interp *interp) {
    
        if(interp == NULL) {
            return NULL;
        }
    
        Tcl_Obj *objPtr = Tcl_NewObj();
        Tcl_InvalidateStringRep(objPtr);
        if(objPtr) {
    
            // Create the tcl procedure associated with this
            // handle object.
            string command("stringCommand");
            if(createTclObjCommand(interp, command, sclPointObjHandler) == NULL) {
                return NULL;
            }
    
            objPtr->bytes = NULL;
            objPtr->length = 0;
            objPtr->refCount = 0;
            objPtr->internalRep.longValue = handle;
            objPtr->typePtr = &sclPointType;
            return objPtr;
        }
        return NULL;
    }
    
    void sclPointUpdateStringProc(Tcl_Obj *objPtr) {
        cout << "sclPointUpdateStringProc called\n";
        string command("stringCommand");
        int len = command.size();
        objPtr->bytes = Tcl_Alloc(len + 1);
        if(objPtr->bytes) {
            memcpy(objPtr->bytes, command.c_str(), len);
            objPtr->bytes[len] = '\0';
            objPtr->length = len;
        }
    }
    
    int deleteTclCommand(Tcl_Interp *interp, const string &cmdName) {
        return(Tcl_DeleteCommand(interp, const_cast<char*>(cmdName.c_str())));
    }
    
    void sclPointFreeIntRepProc(Tcl_Obj *objPtr) {
        cout << "sclPointFreeIntRepProc called\n";
        deleteTclCommand(interp, "stringCommand");
    }
    
    void sclPointDupIntRepProc(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr) {
        cout << "sclPointDupIntRepProc called\n";
        dupPtr->internalRep.longValue = srcPtr->internalRep.longValue;
        dupPtr->typePtr = &sclPointType;
    }
    
    int sclPointSetFromAnyProc(Tcl_Interp *interp, Tcl_Obj *objPtr) {
      cout << "sclPointSetFromAnyProc called\n";
      return TCL_ERROR;
    }
    
    int createVar(ClientData clientData,
      Tcl_Interp *interp, int objc, Tcl_Obj * CONST objv[]) {
    
      cout << "Inside createVar\n";
    
      Tcl_Obj *o = sclNewPointObj(1, interp);
      setTclVariable(interp, "cmd", o);
    
      return TCL_OK;
    }
    
    main() {
      interp = Tcl_CreateInterp();
      if(!interp) {
        cout << "Error creating tcl interpreter\n";
        exit(-1);
      }
    
      Tcl_RegisterObjType(&sclPointType);
    
      createTclCommand(interp, "createVar", createVar, 
        (ClientData)NULL, (Tcl_CmdDeleteProc *)NULL);
    
      string command("createVar; $cmd");
      Tcl_Obj *obj_ptr = newTclStringObj(command);
      if(Tcl_EvalObj(interp, obj_ptr) != TCL_OK) {
        char *result_str;
        Tcl_Obj *result_obj = Tcl_GetObjResult(interp);
        if(result_obj != NULL) {
          int length;
          result_str = Tcl_GetStringFromObj(result_obj, &length);
          if(result_str != NULL && strcmp("SSI_EXIT", result_str) == 0) {
            cout << "Result string is null\n";
            exit(-1);
          }
        }
        cout << "Error in EvalObj: " << result_str << endl;
        exit(-1);
      }
    
      exit(0);
    }
    
    ObservedBehavior:
    I can't figure out how to work around the attached coding problem.  I'm
    creating a variable ($cmd in this case) in the tcl interpreter with the
    value of "stringCommand". I then create a command in the interpreter
    called "stringCommand" and try to invoke it by doing $cmd. The problem
    is that tcl deletes the $cmd variable before I can use it (I think it
    might do this for optimisation because of the existing "stringCommand"
    command). I can get it to work by commenting out line 106 (where it
    calls the sclPointfreeIntRepProc callback) but then the variable and command
    don't get cleaned up automatically by the reference counting code
    when/if the variable goes out of scope.
    
    DesiredBehavior:
    The $cmd variable should not be deleted by tcl until it is not being referenced.
    
    
    
    The C++ code is not easy to read through, but it looks like it
    is missing an Tcl_IncrRefCount's on the objects to make them persist. 
    -- 05/10/2000 hobbs
    
  4. foundin changed to: "final: 8.2.3"
  5. priority changed to: "5 Medium"
  6. resolution changed to: "None"
  7. status changed to: "Open"
  8. submitter changed to: "nobody"
  9. subsystem changed to: "10. Objects"
  10. title changed to: "Command optimisation problem?"
  11. type changed to: "Bug"