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

前端 未结 2 358
予麋鹿
予麋鹿 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

    0 讨论(0)
  • 2021-01-25 02:22

    Module in Lua is just another table with all the functions and variables inside. If you set it to nil and remove the entry in package.loaded it should be cleaned by the garbage collector.

    Here's a function implementing module unloading: http://lua-users.org/lists/lua-l/2009-03/msg00587.html

    0 讨论(0)
提交回复
热议问题