简体   繁体   中英

How To delete a tcl Interpreter?

I am having an application in c++ and I am using tcl interpreter for this.I have created a tcl interpreter for my application using

Tcl_Interp *_tclInterp = Tcl_CreateInterp();

And I have intialized this interpreter in a function. I want to delete/unset the interpreter at last inside the same function in which I have intialized it. So that when I do a

Get("tcl"); 

call I donot get the same interpreter.

So my question is how to delete/unset the tcl interpreter.

You must not use the C++ delete operator on a Tcl interpreter. Tcl's implementation is in C (for various reasons that don't matter to this question) so you must use C idioms when interacting with it. In particular, Tcl_CreateInterp is paired with Tcl_DeleteInterp ; they're even documented on the same manual page . (Tcl doesn't guarantee to free the memory instantly when you delete the interpreter with that function, as it waits until there are no more uses of it on the stack, but if you're deleting it at a sane point then you'll be fine. Internally, Tcl_DeleteInterp eventually ends up calling free() on the interpreter structure, which is correct because the interpreter was allocated via malloc() .) If you want RAII-style memory management, you'll have to wrap it in a small class (a Boost scoped_ptr is almost perfect, except that you need a different destructor).

The other thing to watch out for from C++ is that Tcl interpreters are strictly thread bound; the Tcl library makes extensive use of thread-local data internally to reduce the number of global locks to a bare minimum (in particular, Tcl doesn't have any of the weird global lock problems of the C implementation of Python). An interpreter can only ever be accessed or deleted from the same thread that created it.

As actual rightly points out, the correct function to call is Tcl_DeleteInterp , but there's more to it than that ( man 3tcl Tcl_DeleteInterp ):

Tcl_DeleteInterp marks an interpreter as deleted; the interpreter will eventually be deleted when all calls to Tcl_Preserve for it have been matched by calls to Tcl_Release.

So if you're not doing any reference counting on your interpreter ( Tcl_Preserve / Tcl_Release ), then Tcl_DeleteInterp will delete your interpreter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM