How to drop all code and memory space of a Lua module

前端 未结 2 359
予麋鹿
予麋鹿 2021-01-25 01:35

I have a long time fly program, it is a player. After it started, it will load and run Lua code from net by the server\'s command. Each code have a unique named module.

2条回答
  •  一向
    一向 (楼主)
    2021-01-25 02:14

    For modules written in Lua, setting the entry in package.loaded to nil would probably work. However, it is a hack, and shouldn't be used for arbitrary modules (especially C modules)

    From Mike Pall, developer of LuaJIT:

    Actually, there's no safe way to unload arbitrary modules, even for plain Lua. C modules may depend on each other and unloading them in the wrong order will wreak havoc. And if there's still a userdata with a __gc C function around and you remove the shared library from the address space with the above method ... guess what happens.

    Source

    For your case, consider not using module and require, and instead make your own, simpler system that utilizes functions like loadstring

提交回复
热议问题