Note that there are some explanatory texts on larger screens.

plurals
  1. POLua (LuaJit) and object lifetime in C
    text
    copied!<p>I use <em>LuaJit</em> for extending a plain C application (using the Lua C API). The host application does manage memory for a lot of objects that I have written wrappers for in Lua.</p> <p>Now I would like to be able to delete objects from within the lua function, i.e. implement a delete function. I would like to illustrate the problem at hand with the following outline of the problem.</p> <p>Basically my lua user data structure looks something like this.</p> <pre class="lang-c prettyprint-override"><code>struct my_lua_container { size_t obj_db_index; }; </code></pre> <p>where <code>obj_db_index</code> is an index for the local object database. With the Lua C API I have created a lua-function <code>query_object(...)</code> that retrieves a lua metatable based on this user data and offering an API for managing the db object.</p> <p>I am now planning to introduce a method <code>my_db_object:delete()</code> in the metatable API. <code>:delete()</code> could invalidate <code>my_db_object</code> by overwriting the variable with <code>0</code> or setting another member variable. The problem however is, that all references to the deleted object ought to be invalidated. Consider this lua code:</p> <pre class="lang-lua prettyprint-override"><code>local p = query_object("1") local q = query_object("1") p:delete() q:do_something() -- &lt;=== q still contains a obj_db_index </code></pre> <p>Now I wonder how to solve this potential conflict. The two main problems are:</p> <ul> <li><p>An invalid <code>obj_db_index</code> could be an invalid index. This is actually probably caught by the code already, so it isn't pretty but alright</p></li> <li><p>after deletion, the index might be reused and this could lead to subtle errors when other references still use the old index.</p></li> </ul> <p><strong>What are strategies to deal with this?</strong> </p> <p>My idea might be a little time-consuming but this would be ok in the event of deletion:</p> <ul> <li>Is there some Introspection that I can perform on user data objects? Like iterating over all user data objects with the same type in order to invalidate <code>my_db_index</code> when the deletion is triggered</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload