I am using LuaJit to extend a simple C application (using the Lua C API). The host application does manage memory for a lot of objects, which I wrote wrappers for Lua on.
Now, I would like to be able to remove objects from the lua function, i.e. implement the delete function. I would like to illustrate this problem with the following outline of the problem.
Basically my custom lua data structure looks something like this.
struct my_lua_container { size_t obj_db_index; };
where obj_db_index
is the index for the database of local objects. With the Lua C API, I created the lua function query_object(...)
, which extracts the lua metatable based on this user data and offers an API to manage the db object.
Now I plan to introduce the my_db_object:delete()
method into the metatable API. :delete()
can invalidate my_db_object
by overwriting the variable with 0
or setting another member variable. However, the problem is that all references to the remote object must be invalid. Consider this lua code:
local p = query_object("1") local q = query_object("1") p:delete() q:do_something() -- <=== q still contains a obj_db_index
Now I am wondering how to resolve this potential conflict. Two main problems:
Invalid obj_db_index
may be an invalid index. In fact, it probably already caught the code, so it's not very, but good
after deletion, the index can be reused, and this can lead to subtle errors when other links still use the old index.
What are the strategies to solve this problem?
My idea may be a little time consuming, but that would be fine if removed:
- Is there some kind of Introspection that I can perform on user data objects? Like iterating over all user data objects of the same type, to invalidate
my_db_index
when the deletion starts
source share