Tcl Source Code

Hex Artifact Content
Login

Artifact bd736e226df5c047768849f40e19666b0c7ff6fc:

Attachment "tclleak.c" to ticket [1075378fff] added by mteira 2004-11-29 23:55:33.
0000: 23 69 6e 63 6c 75 64 65 20 3c 73 74 64 6c 69 62  #include <stdlib
0010: 2e 68 3e 0a 23 69 6e 63 6c 75 64 65 20 3c 75 6e  .h>.#include <un
0020: 69 73 74 64 2e 68 3e 0a 23 69 6e 63 6c 75 64 65  istd.h>.#include
0030: 20 3c 6c 69 62 67 65 6e 2e 68 3e 0a 23 69 6e 63   <libgen.h>.#inc
0040: 6c 75 64 65 20 3c 70 74 68 72 65 61 64 2e 68 3e  lude <pthread.h>
0050: 0a 23 69 6e 63 6c 75 64 65 20 3c 74 63 6c 2e 68  .#include <tcl.h
0060: 3e 0a 0a 73 74 61 74 69 63 20 63 68 61 72 20 2a  >..static char *
0070: 70 4e 61 6d 65 3b 0a 73 74 61 74 69 63 20 63 68  pName;.static ch
0080: 61 72 20 2a 73 63 72 69 70 74 5f 6e 61 6d 65 20  ar *script_name 
0090: 3d 20 22 74 65 73 74 2e 74 63 6c 22 3b 0a 73 74  = "test.tcl";.st
00a0: 61 74 69 63 20 63 68 61 72 20 2a 70 72 6f 63 5f  atic char *proc_
00b0: 6e 61 6d 65 20 3d 20 22 74 65 73 74 22 3b 0a 0a  name = "test";..
00c0: 73 74 61 74 69 63 20 76 6f 69 64 20 2a 74 68 46  static void *thF
00d0: 75 6e 63 74 69 6f 6e 28 20 76 6f 69 64 20 2a 70  unction( void *p
00e0: 20 29 20 0a 7b 0a 20 20 54 63 6c 5f 49 6e 74 65   ) .{.  Tcl_Inte
00f0: 72 70 20 2a 69 6e 74 65 72 70 20 3d 20 54 63 6c  rp *interp = Tcl
0100: 5f 43 72 65 61 74 65 49 6e 74 65 72 70 28 29 3b  _CreateInterp();
0110: 0a 20 20 54 63 6c 5f 4f 62 6a 20 2a 70 53 63 72  .  Tcl_Obj *pScr
0120: 69 70 74 20 3d 20 4e 55 4c 4c 3b 0a 20 20 69 66  ipt = NULL;.  if
0130: 20 28 20 69 6e 74 65 72 70 20 29 20 7b 0a 20 20   ( interp ) {.  
0140: 20 20 54 63 6c 5f 49 6e 69 74 28 20 69 6e 74 65    Tcl_Init( inte
0150: 72 70 20 29 3b 0a 20 20 20 20 54 63 6c 5f 50 72  rp );.    Tcl_Pr
0160: 65 73 65 72 76 65 28 20 69 6e 74 65 72 70 20 29  eserve( interp )
0170: 3b 0a 20 20 20 20 0a 20 20 20 20 69 66 20 28 20  ;.    .    if ( 
0180: 54 63 6c 5f 45 76 61 6c 46 69 6c 65 28 20 69 6e  Tcl_EvalFile( in
0190: 74 65 72 70 2c 20 73 63 72 69 70 74 5f 6e 61 6d  terp, script_nam
01a0: 65 20 29 20 21 3d 20 54 43 4c 5f 4f 4b 20 29 20  e ) != TCL_OK ) 
01b0: 7b 0a 20 20 20 20 20 20 66 70 72 69 6e 74 66 28  {.      fprintf(
01c0: 20 73 74 64 65 72 72 2c 20 22 45 72 72 6f 72 20   stderr, "Error 
01d0: 65 76 61 6c 75 61 74 69 6e 67 20 25 73 28 25 64  evaluating %s(%d
01e0: 29 3a 20 25 73 5c 6e 22 2c 0a 20 20 20 20 20 20  ): %s\n",.      
01f0: 20 20 20 20 20 20 20 20 20 73 63 72 69 70 74 5f           script_
0200: 6e 61 6d 65 2c 20 69 6e 74 65 72 70 2d 3e 65 72  name, interp->er
0210: 72 6f 72 4c 69 6e 65 2c 0a 20 20 20 20 20 20 20  rorLine,.       
0220: 20 20 20 20 20 20 20 20 54 63 6c 5f 47 65 74 53          Tcl_GetS
0230: 74 72 69 6e 67 52 65 73 75 6c 74 28 20 69 6e 74  tringResult( int
0240: 65 72 70 20 29 20 29 3b 0a 20 20 20 20 7d 20 65  erp ) );.    } e
0250: 6c 73 65 20 7b 0a 20 20 20 20 20 20 54 63 6c 5f  lse {.      Tcl_
0260: 4f 62 6a 20 2a 70 53 63 72 69 70 74 20 3d 20 54  Obj *pScript = T
0270: 63 6c 5f 4e 65 77 53 74 72 69 6e 67 4f 62 6a 28  cl_NewStringObj(
0280: 20 70 72 6f 63 5f 6e 61 6d 65 2c 0a 20 20 20 20   proc_name,.    
0290: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02a0: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20                  
02b0: 20 20 20 20 20 20 20 73 74 72 6c 65 6e 28 20 70         strlen( p
02c0: 72 6f 63 5f 6e 61 6d 65 20 29 20 29 3b 0a 20 20  roc_name ) );.  
02d0: 20 20 20 20 54 63 6c 5f 49 6e 63 72 52 65 66 43      Tcl_IncrRefC
02e0: 6f 75 6e 74 28 20 70 53 63 72 69 70 74 20 29 3b  ount( pScript );
02f0: 0a 20 20 20 20 20 20 69 66 20 28 20 54 63 6c 5f  .      if ( Tcl_
0300: 45 76 61 6c 4f 62 6a 45 78 28 20 69 6e 74 65 72  EvalObjEx( inter
0310: 70 2c 20 70 53 63 72 69 70 74 2c 20 30 20 29 20  p, pScript, 0 ) 
0320: 21 3d 20 54 43 4c 5f 4f 4b 20 29 20 7b 0a 20 20  != TCL_OK ) {.  
0330: 20 20 20 20 20 20 66 70 72 69 6e 74 66 28 20 73        fprintf( s
0340: 74 64 65 72 72 2c 20 22 45 72 72 6f 72 20 63 61  tderr, "Error ca
0350: 6c 6c 69 6e 67 20 25 73 28 25 64 29 3a 20 25 73  lling %s(%d): %s
0360: 5c 6e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20  \n",.           
0370: 20 20 20 20 20 20 70 72 6f 63 5f 6e 61 6d 65 2c        proc_name,
0380: 20 69 6e 74 65 72 70 2d 3e 65 72 72 6f 72 4c 69   interp->errorLi
0390: 6e 65 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20  ne,.            
03a0: 20 20 20 20 20 54 63 6c 5f 47 65 74 53 74 72 69       Tcl_GetStri
03b0: 6e 67 52 65 73 75 6c 74 28 20 69 6e 74 65 72 70  ngResult( interp
03c0: 20 29 20 29 3b 0a 20 20 20 20 20 20 7d 0a 20 20   ) );.      }.  
03d0: 20 20 20 20 54 63 6c 5f 44 65 63 72 52 65 66 43      Tcl_DecrRefC
03e0: 6f 75 6e 74 28 20 70 53 63 72 69 70 74 20 29 3b  ount( pScript );
03f0: 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 44 65 73  .    }.    /*Des
0400: 74 72 6f 79 20 74 68 65 20 69 6e 74 65 72 70 72  troy the interpr
0410: 65 74 65 72 2a 2f 0a 20 20 20 20 69 66 20 28 20  eter*/.    if ( 
0420: 21 54 63 6c 5f 49 6e 74 65 72 70 44 65 6c 65 74  !Tcl_InterpDelet
0430: 65 64 28 20 69 6e 74 65 72 70 20 29 20 29 20 7b  ed( interp ) ) {
0440: 0a 20 20 20 20 20 20 54 63 6c 5f 44 65 6c 65 74  .      Tcl_Delet
0450: 65 49 6e 74 65 72 70 28 20 69 6e 74 65 72 70 20  eInterp( interp 
0460: 29 3b 0a 20 20 20 20 20 20 54 63 6c 5f 52 65 6c  );.      Tcl_Rel
0470: 65 61 73 65 28 20 28 43 6c 69 65 6e 74 44 61 74  ease( (ClientDat
0480: 61 29 20 69 6e 74 65 72 70 20 29 3b 20 0a 20 20  a) interp ); .  
0490: 20 20 7d 20 65 6c 73 65 20 7b 0a 20 20 20 20 20    } else {.     
04a0: 20 66 70 72 69 6e 74 66 28 20 73 74 64 65 72 72   fprintf( stderr
04b0: 2c 20 22 49 6e 74 65 72 70 20 61 6c 72 65 61 64  , "Interp alread
04c0: 79 20 64 65 6c 65 74 65 64 21 21 21 5c 6e 22 20  y deleted!!!\n" 
04d0: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 54 63 6c  );.    }.    Tcl
04e0: 5f 46 69 6e 61 6c 69 7a 65 54 68 72 65 61 64 28  _FinalizeThread(
04f0: 29 3b 0a 20 20 7d 0a 20 20 72 65 74 75 72 6e 28  );.  }.  return(
0500: 20 4e 55 4c 4c 20 29 3b 0a 7d 0a 20 20 20 20 0a   NULL );.}.    .
0510: 20 20 20 20 20 0a 69 6e 74 20 6d 61 69 6e 28 20       .int main( 
0520: 69 6e 74 20 61 72 67 63 2c 20 63 68 61 72 2a 2a  int argc, char**
0530: 20 61 72 67 76 20 29 0a 7b 0a 20 20 69 6e 74 20   argv ).{.  int 
0540: 69 3b 0a 20 20 69 6e 74 20 6a 3b 0a 20 20 70 74  i;.  int j;.  pt
0550: 68 72 65 61 64 5f 74 20 2a 61 54 68 72 65 61 64  hread_t *aThread
0560: 73 3b 0a 20 20 69 6e 74 20 6e 54 68 72 65 61 64  s;.  int nThread
0570: 73 3b 0a 20 20 69 6e 74 20 6e 49 74 65 72 73 3b  s;.  int nIters;
0580: 0a 20 20 70 4e 61 6d 65 20 3d 20 62 61 73 65 6e  .  pName = basen
0590: 61 6d 65 28 20 61 72 67 76 5b 30 5d 20 29 3b 0a  ame( argv[0] );.
05a0: 0a 20 20 69 66 28 20 61 72 67 63 20 3c 20 33 20  .  if( argc < 3 
05b0: 29 20 7b 0a 20 20 20 20 66 70 72 69 6e 74 66 28  ) {.    fprintf(
05c0: 20 73 74 64 65 72 72 2c 20 22 55 73 61 67 65 3a   stderr, "Usage:
05d0: 20 25 73 20 6e 54 68 72 65 61 64 73 20 6e 49 74   %s nThreads nIt
05e0: 65 72 73 5c 6e 22 2c 20 70 4e 61 6d 65 20 29 3b  ers\n", pName );
05f0: 0a 20 20 20 20 72 65 74 75 72 6e 20 28 2d 31 29  .    return (-1)
0600: 3b 0a 20 20 7d 0a 20 20 6e 54 68 72 65 61 64 73  ;.  }.  nThreads
0610: 20 3d 20 61 74 6f 69 28 20 61 72 67 76 5b 31 5d   = atoi( argv[1]
0620: 20 29 3b 0a 20 20 6e 49 74 65 72 73 20 3d 20 61   );.  nIters = a
0630: 74 6f 69 28 20 61 72 67 76 5b 32 5d 20 29 3b 0a  toi( argv[2] );.
0640: 0a 20 20 61 54 68 72 65 61 64 73 20 3d 20 28 20  .  aThreads = ( 
0650: 70 74 68 72 65 61 64 5f 74 20 2a 20 29 20 6d 61  pthread_t * ) ma
0660: 6c 6c 6f 63 28 20 6e 54 68 72 65 61 64 73 20 2a  lloc( nThreads *
0670: 20 73 69 7a 65 6f 66 28 20 70 74 68 72 65 61 64   sizeof( pthread
0680: 5f 74 20 29 20 29 3b 0a 0a 20 20 66 6f 72 28 20  _t ) );..  for( 
0690: 6a 3d 30 3b 20 6a 3c 20 6e 49 74 65 72 73 3b 20  j=0; j< nIters; 
06a0: 6a 2b 2b 20 29 20 7b 0a 20 20 20 20 66 6f 72 28  j++ ) {.    for(
06b0: 20 69 3d 30 3b 20 69 3c 20 6e 54 68 72 65 61 64   i=0; i< nThread
06c0: 73 3b 20 69 2b 2b 20 29 20 7b 0a 20 20 20 20 20  s; i++ ) {.     
06d0: 20 70 74 68 72 65 61 64 5f 63 72 65 61 74 65 28   pthread_create(
06e0: 20 26 61 54 68 72 65 61 64 73 5b 69 5d 2c 20 30   &aThreads[i], 0
06f0: 2c 20 74 68 46 75 6e 63 74 69 6f 6e 2c 20 30 20  , thFunction, 0 
0700: 29 3b 0a 20 20 20 20 7d 0a 20 20 20 20 2f 2a 20  );.    }.    /* 
0710: 57 61 69 74 20 66 6f 72 20 74 68 65 20 74 68 72  Wait for the thr
0720: 65 61 64 73 20 74 6f 20 65 6e 64 20 2a 2f 0a 20  eads to end */. 
0730: 20 20 20 66 6f 72 28 20 69 20 3d 20 30 3b 20 69     for( i = 0; i
0740: 20 3c 20 6e 54 68 72 65 61 64 73 3b 20 69 2b 2b   < nThreads; i++
0750: 20 29 20 7b 0a 20 20 20 20 20 20 70 74 68 72 65   ) {.      pthre
0760: 61 64 5f 6a 6f 69 6e 28 20 61 54 68 72 65 61 64  ad_join( aThread
0770: 73 5b 69 5d 2c 20 30 20 29 3b 0a 20 20 20 20 7d  s[i], 0 );.    }
0780: 0a 20 20 7d 0a 20 20 66 72 65 65 28 20 61 54 68  .  }.  free( aTh
0790: 72 65 61 64 73 20 29 3b 0a 20 20 72 65 74 75 72  reads );.  retur
07a0: 6e 28 20 30 20 29 3b 0a 7d 0a                    n( 0 );.}.