Note that there are some explanatory texts on larger screens.

plurals
  1. POLua weak references
    primarykey
    data
    text
    <p>I'm working on a project in Lua where I will be creating tables and storing them in a master table, to be erased at a later time. I will pass around references to these tables to other sibling tables.</p> <pre><code>master = {} table.insert(master, {name = 'hello'}) table.insert(master, {name = 'world', pre = master[1]}) </code></pre> <p>The problem that occurs is that when I wish to erase the reference from the master table, the reference still remains in master[2] here. Obviously my first solution was to make the tables have weak values. (through .__mode on a metatable, not shown here)</p> <p>This worked, and would work, so long as I would never store a singly-referenced table within these tables.</p> <pre><code>table.insert(master, {name = 'goodbye', pre = master[2], some_table = {123}}) </code></pre> <p>The third element, <code>some_table</code> would eventually be collected, because the tables have weak values, and this table (<code>some_table</code>) is not referenced anywhere else. This is undesired behavior. My latest solution involves creating "weak reference objects" to the tables within the master table. A naive implementation follows:</p> <pre><code>function WeakRef(t) r = {__mode = 'v', __index = t, __newindex = t} setmetatable(r, r) return r end </code></pre> <p>These weak reference objects act similarly to <code>boost::weak_ptr</code>s and accomplish my goal, but I am uncertain if they are the best solution to my problem.</p> <p>Is there a better way; a more elegant solution? Is my design, which requires this master table, perhaps flawed?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. CO"The problem that occurs is that when I wish to erase the reference from the master table, the reference still remains in master[2] here." I'm curious as to why this is a problem. Does other code _truly_ need to be notified as to the removal of the object from the master table? Because the GC doesn't guarantee a timeframe for when these things get collected. What exactly is the design intended to do that requires such tight memory control in Lua?
      singulars
    2. COThey do not necessarily need to be notified right away, per se. The intended design is a master array indicating physical objects within a simulation, and when the object is no longer necessary, it may be removed. It is similar to having a set of strong pointers in one array, then spreading around weak pointers to the objects. My problem is that I am giving objects references to other objects and this can hold up collection if I wish to immediately remove an object from the simulation. Does this make sense?
      singulars
    3. COI understand what you're doing. I'm not sure _why_ you're doing it. Since weak references are not guaranteed to immediately disappear, what does it matter if they do or not? That's the part I don't understand: why do the other references need to be weak? Weak references will not prevent accidental use of a dead object, since the GC may not have collected it yet. The only reasons for them to be weak is either to prevent circular dependencies (which Lua doesn't have a problem with) or to manage memory more efficiently. Are you allocating enough data that memory management is an issue?
      singulars
 

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