Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: e304fbd6c882775e06593537e6f8220208ca9479
Ticket: 217991ffffffffffffffffffffffffffffffffff
bug in TCL_OBJ and dynamic loading
User & Date: nobody on 2000-10-26 00:51:45
Changes

  1. assignee changed to: "nobody"
  2. cmimetype changed to: "text/plain"
  3. comment changed to:
    OriginalBugID: 574 Bug
    Version: 8.0p2
    SubmitDate: '1998-05-21'
    LastModified: '1999-10-21'
    Severity: LOW
    Status: Closed
    Submitter: hershey
    ChangedBy: hobbs
    OS: All Unix
    Machine: NA
    FixedDate: '2000-10-25'
    FixedInVersion: NA
    ClosedDate: '1999-10-21'
    
    
    I already posted this problem a month ago. I tried to locate the
    problem but I couldn't succeed yet (also in lack of time). So
    here again the problem (BTW, tcl8.1a2 shows the same problem):
    
    ------------
    
    I've a problem using my own tcl objects. The problem arises if I
    spread things over different shared objects and if I use
    "package require" to load the shared objects.
    My working env. is tcl8.0p2 on an Irix6.2 System (but I get
    the same problem on a Dec with DU 4.0). I've listed the source
    code
    below (s1.c, s2.c, tob.h, makefile, test.tcl)
    
    I have one shared lib which implements a tcl object (i.e.
    implements
    all things required by "Tcl_ObjType" + a Tcl_GetTobFromObj) and
    provides
    this as package PkgA. The tcl interface "NewTob" creates a new
    instance of the object (see s1.c)
    
    And then I have a second shared lib which accesses such an object
    and provides package PkgB. The tcl interface "ManiTob" tries to
    use this package (see s2.c)
    
    Now using tclsh8.0 with:
    
    lappend auto_path .
    package require PkgA 1.0
    package require PkgB 1.0
    
    set a [NewTob] ; # creates a new test object
    puts "a: $a"   ; # prints the correct string rep.
    
    ManiTob $a ; # accesses $a via my Tcl_GetTobFromObj() BUT FAILS
    here
               ; # $a is not a valid test object
    
    #SURPRISINGLY a second call
    
    ManiTob $a ; #now does the right thing.
    
    ----------
    And also a surprise:
    If I replace the "package require" commands by "load" commands
    also
    the first call is ok !!
    
    e.g. this works:
    
    load ./pkga.so
    load ./pkgb.so
    set a [NewTob]
    ManiTob $a
    
    or this works:
    
    load ./pkga.so
    set a [NewTob]
    load ./pkgb.so
    ManiTob $a
    
    I don't know if there is something wrong with my source code. But
    in this case I guess it's rather unlikely that the thing works
    when
    I use the load command.
    
    Does anybody have an idea what goes wrong? Is this a bug in the
    "package" command?
    
    Thanks for any hint,
    -Peter
    
    
    
    
    The source code:
    ------------------------------ tob.h
    ------------------------------------------
    
    /* the test object */
    typedef struct Tob {
      char   s[10];
      int    i;
      float  f;
    } Tob;
    
    
    Tob *TobAlloc();       /* alloc a tob */
    Tob *TobFree(Tob *t);  /* free a tob */
    Tob *TobDup(Tob *t);   /* duplicate a tob */
    
    /* get the tob from a Tcl_Object */
    int Tcl_GetTobFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr, Tob
    **tobPtr);
    
    ------------------------------ s1.c
    -------------------------------------------
    #include <tcl.h>
    #include <string.h>
    #include "tob.h"
    
    /* alloc, free and copy a tob */
    Tob *TobAlloc() { return (Tob*) calloc(1,sizeof(Tob)); }
    Tob *TobFree(Tob *t) { free(t); return NULL; }
    Tob *TobDup(Tob *t) { Tob *d; d = TobAlloc(); *d = *t; }
    
    static void   DupTobInternalRep _ANSI_ARGS_((Tcl_Obj *srcPtr,
                                Tcl_Obj *copyPtr));
    static void   FreeTobInternalRep _ANSI_ARGS_((Tcl_Obj *objPtr));
    static int    SetTobFromAny _ANSI_ARGS_((Tcl_Interp *interp,
                                Tcl_Obj *objPtr));
    static void   UpdateStringOfTob _ANSI_ARGS_((Tcl_Obj *objPtr));
    
    
    Tcl_ObjType tclTobType = {
        "top",                             /* name */
        FreeTobInternalRep,                /* freeIntRepProc */
        DupTobInternalRep,                 /* dupIntRepProc */
        UpdateStringOfTob,                 /* updateStringProc */
        SetTobFromAny                      /* setFromAnyProc */
    };
    
    
    static void   DupTobInternalRep (Tcl_Obj *srcPtr, Tcl_Obj
    *copyPtr)
    {
      fprintf(stderr,"duplication of %s\n",srcPtr->bytes);
    fflush(stderr);
    
      copyPtr->internalRep.otherValuePtr =
        TobDup((Tob *) srcPtr->internalRep.otherValuePtr);
      copyPtr->typePtr = &tclTobType;
    }
    
    
    static void   FreeTobInternalRep (Tcl_Obj *objPtr)
    {
      Tob *t  = (Tob *) objPtr->internalRep.otherValuePtr;
      TobFree(t);
      fprintf(stderr,"freeing of %s\n",objPtr->bytes); fflush(stderr);
    
    }
    
    static void UpdateStringOfTob(Tcl_Obj *objPtr)
    {
      char b[100];
      Tob *t = objPtr->internalRep.otherValuePtr;
      if ( objPtr->bytes != NULL ) {
        fprintf(stderr,
                "UpdateStringOfAtom frees old string
    rep\n%s\n",objPtr->bytes);
        fflush(stderr);
        free( objPtr->bytes );
      }
    
      fprintf(stderr,"Update string....\n"); fflush(stderr);
    
      /* print address , string , int value and float value */
      sprintf(b,"%X %s %d %f",t,t->s,t->i,t->f);
      objPtr->bytes = strdup( b);
      objPtr->length = strlen( objPtr->bytes );
    }
    
    /* just to be here */
    static int SetTobFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr)
    {
      Tcl_Obj     *resultPtr;
      resultPtr = Tcl_GetObjResult( interp );
      Tcl_SetStringObj( resultPtr, "Can't set tob from string", -1 );
      return TCL_ERROR;
    }
    
    /* interface to create a new tob */
    static int NewTob(ClientData D,Tcl_Interp *IP, int objc, Tcl_Obj
    *CONST objv[])
    {
      Tob *t        = TobAlloc();
      Tcl_Obj  *to  = Tcl_GetObjResult(IP);
    
      strcpy(t->s,"text");
      t->i = 10;
      t->f = 3.1415;
      to->typePtr = &tclTobType;
      to->internalRep.otherValuePtr = t;
      Tcl_InvalidateStringRep(to);
    
      return TCL_OK;
    }
    
    int Tcl_GetTobFromObj(Tcl_Interp *interp, Tcl_Obj *objPtr,Tob
    **tobPtr)
    {
    
      *tobPtr = (Tob*) objPtr->internalRep.otherValuePtr;
    
      /* check if this is really a test object */
      if (objPtr->typePtr != &tclTobType) {
        int      len;
    
        Tcl_Obj *resultPtr = Tcl_GetObjResult(interp);
        Tcl_AppendStringsToObj( resultPtr, "Not a Tob: ",
                                Tcl_GetStringFromObj(objPtr,&len),
    NULL );
        fprintf(stderr,"objPtr->typePtr should be %X but is %X\n",
                &tclTobType,objPtr->typePtr);
    
        return TCL_ERROR;
      }
      return TCL_OK;
    }
    
    
    int Pkga_Init(Tcl_Interp *IP)
    {
      Tcl_PkgProvide( IP, "PkgA", "1.0" );
      Tcl_CreateObjCommand(IP, "NewTob" ,NewTob,NULL,NULL);
    
      return TCL_OK;
    }
    
    ------------------------------ s2.c
    -------------------------------------------
    #include <tcl.h>
    #include "tob.h"
    
    static int ManipulateTob(ClientData D, Tcl_Interp *IP, int objc,
                             Tcl_Obj *CONST objv[])
    {
      int error;
      Tob *t;
    
      error = Tcl_GetTobFromObj( IP, objv[1], &t );
      if ( error != TCL_OK ) return error;
    
      t->i = 666; /* just a test */
      Tcl_InvalidateStringRep(objv[1]);
      return TCL_OK;
    }
    
    
    int Pkgb_Init(Tcl_Interp *IP)
    {
      Tcl_PkgProvide( IP, "PkgB", "1.0" );
      Tcl_CreateObjCommand(IP, "ManiTob" ,ManipulateTob,NULL,NULL);
    
      return TCL_OK;
    }
    
    ------------------------------ makefile
    --------------------------------------
    all: s1.o s2.o
            ld -shared -all s1.o  -o pkga.so
            ld -shared -all s2.o  -o pkgb.so
            (echo 'pkg_mkIndex . *.so ' | tclsh8.0) ;
    
    s1.o: s1.c tob.h
            cc -c -g s1.c -I/usr/local/include
    
    s2.o: s2.c tob.h
            cc -c -g s2.c -I/usr/local/include
    
    
    ------------------------------ test.tcl
    --------------------------------------
    lappend auto_path .
    package require PkgA 1.0
    package require PkgB 1.0
    
    # create a new test object
    set a [NewTob]
    # print its string representation
    puts "a: $a"
    
    # Manipulate test object
    # this FAILS
    ManiTob $a
    
    # Manipulate test object
    # this succeeds
    ManiTob $a
    
    |                   Peter Lackner                 |
    |    Center of Applied Molecular Engineering      |
    |          University of Salzburg, Austria        |
    |            [email protected]         |
    
    This appears to be an instance where the package must be
    loaded first, for which the pkg_mkIndex has the -direct
    flag, to be used when creating the pkgIndex.tcl file.
    I believe this was added somewhere in 8.1. 
    -- 10/21/1999 hobbs
    
  4. foundin changed to: "obsolete: 8.0p2"
  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: "bug in TCL_OBJ and dynamic loading"
  11. type changed to: "Bug"