Tcl Source Code

Artifact [bd736e226d]
Login

Artifact bd736e226df5c047768849f40e19666b0c7ff6fc:

Attachment "tclleak.c" to ticket [1075378fff] added by mteira 2004-11-29 23:55:33.
#include <stdlib.h>
#include <unistd.h>
#include <libgen.h>
#include <pthread.h>
#include <tcl.h>

static char *pName;
static char *script_name = "test.tcl";
static char *proc_name = "test";

static void *thFunction( void *p ) 
{
  Tcl_Interp *interp = Tcl_CreateInterp();
  Tcl_Obj *pScript = NULL;
  if ( interp ) {
    Tcl_Init( interp );
    Tcl_Preserve( interp );
    
    if ( Tcl_EvalFile( interp, script_name ) != TCL_OK ) {
      fprintf( stderr, "Error evaluating %s(%d): %s\n",
               script_name, interp->errorLine,
               Tcl_GetStringResult( interp ) );
    } else {
      Tcl_Obj *pScript = Tcl_NewStringObj( proc_name,
                                           strlen( proc_name ) );
      Tcl_IncrRefCount( pScript );
      if ( Tcl_EvalObjEx( interp, pScript, 0 ) != TCL_OK ) {
        fprintf( stderr, "Error calling %s(%d): %s\n",
                 proc_name, interp->errorLine,
                 Tcl_GetStringResult( interp ) );
      }
      Tcl_DecrRefCount( pScript );
    }
    /*Destroy the interpreter*/
    if ( !Tcl_InterpDeleted( interp ) ) {
      Tcl_DeleteInterp( interp );
      Tcl_Release( (ClientData) interp ); 
    } else {
      fprintf( stderr, "Interp already deleted!!!\n" );
    }
    Tcl_FinalizeThread();
  }
  return( NULL );
}
    
     
int main( int argc, char** argv )
{
  int i;
  int j;
  pthread_t *aThreads;
  int nThreads;
  int nIters;
  pName = basename( argv[0] );

  if( argc < 3 ) {
    fprintf( stderr, "Usage: %s nThreads nIters\n", pName );
    return (-1);
  }
  nThreads = atoi( argv[1] );
  nIters = atoi( argv[2] );

  aThreads = ( pthread_t * ) malloc( nThreads * sizeof( pthread_t ) );

  for( j=0; j< nIters; j++ ) {
    for( i=0; i< nThreads; i++ ) {
      pthread_create( &aThreads[i], 0, thFunction, 0 );
    }
    /* Wait for the threads to end */
    for( i = 0; i < nThreads; i++ ) {
      pthread_join( aThreads[i], 0 );
    }
  }
  free( aThreads );
  return( 0 );
}