Tcl extensions: Life Cycle of extensions' ClientData

浪尽此生 提交于 2019-12-11 03:45:19

问题


Non-trivial native extensions will require per-interpreter data structures that are dynamically allocated.

I am currently using Tcl_SetAssocData, with a key corresponding to the extension's name and an appropriate deletion routine, to prevent this memory from leaking away.

However, Tcl_PkgProvideEx also allows one to record such information. This information can be retrieved by Tcl_PkgRequireEx. Associating the extension's data structures with its package seems more natural than in the "grab-bag" AssocData; yet the Pkg*Ex routines do not provide an automatically invoked deletion routine. So I think I need to stay with the AssocData approach.

For which situations were the Pkg*Ex routines designed for?

Additionally, the Tcl Library allows one to install ExitHandlers and ThreadExitHandlers. Paraphasing the manual, this is for flushing buffers to disk etc.

Are there any other situations requiring use of ExitHandlers?

When Tcl calls exit, are Tcl_PackageUnloadProcs called?


回答1:


The whole-extension ClientData is intended for extensions that want to publish their own stub table (i.e., an organized list of functions that represent an exact ABI) that other extensions can build against. This is a very rare thing to want to do; leave at NULL if you don't want it (and contact the Tcl core developers' mailing list directly if you do; we've got quite a bit of experience in this area). Since it is for an ABI structure, it is strongly expected to be purely static data and so doesn't need deletion. Dynamic data should be sent through a different mechanism (e.g., via the Tcl interpreter or through calling functions via the ABI).

Exit handlers (which can be registered at multiple levels) are things that you use when you have to delete some resource at an appropriate time. The typical points of interest are when an interpreter (a Tcl_Interp structure) is being deleted, when a thread is being deleted, and when the whole process is going away. What resources need to be specially deleted? Well, it's usually obvious: file handles, database handles, that sort of thing. It's awkward to answer in general as the details matter very much: ask a more specific question to get tailored advice.

However, package unload callbacks are only called in response to the unload command. Like package load callbacks, they use “special function symbol” registration, and if they are absent then the unload command will refuse to unload the package. Most packages do not use them. The use case is where there are very long-lived processes that need to have extra upgradeable functionality added to them.



来源:https://stackoverflow.com/questions/9598753/tcl-extensions-life-cycle-of-extensions-clientdata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!