[Plib-devel] psl copy constructor.
Brought to you by:
sjbaker
From: Jonathan W. <jtw...@in...> - 2004-04-18 10:53:41
|
I've been tinkering with psl scripting and written myself a small manager class that will automatically create script instances using the copy constructor if there is already a compiled psl script in existance. this seems to work quite well until it is time to free the script instances. the first one works OK, but subsequent ones have problems. Looking through the code I found that there is no differentiation between instances and the scripts theey are created from when freeing the compiler and code structures. I've written a little bit of code that changes the pslProgram destructor so that it only frees the pslCode and pslCompiler objects on the master copy of a script. it will still cause problems if someone deletes the master copy of a script and continues to use instances created from it, but I can now create and delete ten instances of a script created using the copy constructor without problems. I have attatched a copy of my script at the end of this E-Mail. I would appreciate any feedback that people have. Thanks, Jonathan. Index: psl.h =================================================================== RCS file: /cvsroot/plib/plib/src/psl/psl.h,v retrieving revision 1.27 diff -u -r1.27 psl.h --- psl.h 6 Apr 2004 12:53:15 -0000 1.27 +++ psl.h 18 Apr 2004 10:16:48 -0000 @@ -301,7 +301,7 @@ int force_trace ; int force_stacktrace ; - + int is_copy; public: pslProgram ( const pslExtension *ext, const char *_progName = NULL ) ; Index: pslProgram.cxx =================================================================== RCS file: /cvsroot/plib/plib/src/psl/pslProgram.cxx,v retrieving revision 1.17 diff -u -r1.17 pslProgram.cxx --- pslProgram.cxx 6 Jan 2003 05:10:14 -0000 1.17 +++ pslProgram.cxx 18 Apr 2004 10:16:48 -0000 @@ -29,7 +29,7 @@ if ( ! _pslInitialised ) ulSetError ( UL_FATAL, "PSL: You didn't call pslInit() before using PSL functions." ) ; - + is_copy = FALSE; code = new pslOpcode [ MAX_CODE ] ; extensions = ext ; @@ -64,12 +64,12 @@ progName = NULL ; if ( _prgnm == NULL ) _prgnm = src -> getProgName () ; - + setProgName ( _prgnm ) ; /* This will fail if this pslProgram is ever deleted */ /* We need ref-counting on code/compiler */ - + is_copy = TRUE; code = src -> getCode () ; compiler = src -> getCompiler () ; extensions = src -> getExtensions () ; @@ -86,9 +86,13 @@ /* We need ref-counting on code/compiler */ /* DEBUG-ME! */ delete [] progName ; - delete compiler ; delete context ; - delete [] code ; + if(!is_copy) + { + delete [] code ; + delete compiler ; + } + } |