Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:17:13+00:00 2026-05-25T15:17:13+00:00

I’m working on a project in Lua where I will be creating tables and

  • 0

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.

master = {}
table.insert(master, {name = 'hello'})
table.insert(master, {name = 'world', pre = master[1]})

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)

This worked, and would work, so long as I would never store a singly-referenced table within these tables.

table.insert(master, {name = 'goodbye', pre = master[2], some_table = {123}})

The third element, some_table would eventually be collected, because the tables have weak values, and this table (some_table) 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:

function WeakRef(t)
    r = {__mode = 'v', __index = t, __newindex = t}
    setmetatable(r, r)
    return r
end

These weak reference objects act similarly to boost::weak_ptrs and accomplish my goal, but I am uncertain if they are the best solution to my problem.

Is there a better way; a more elegant solution?
Is my design, which requires this master table, perhaps flawed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:17:14+00:00Added an answer on May 25, 2026 at 3:17 pm

    Given that:

    1. You want master to be the “one place” where you define whether an object exists or not
    2. Your objects can have links between them

    Then probably the simplest architecture is reserving one of the members of each object as a “middle man” in charge of managing the references to others. Here’re the steps:

    1. Make master a regular table (not weak)
    2. On each physical object, create a weak table called links (or whatever name suits your logic better)
    3. Make all links tables weak. Use them to store references to other objects.

    And this is a possible implementation. I’ve tried it in Lua 5.1:

    local function newWeakTable()
      return setmetatable({}, {__mode = "v"})
    end
    
    local master = {}
    
    -- create two physical objects
    local obj1 = { name = "obj1", links = newWeakTable() }
    local obj2 = { name = "obj2", links = newWeakTable() }
    
    -- link them
    obj2.links.pre = obj1
    
    -- insert them into master
    table.insert(master, obj1)
    table.insert(master, obj2)
    
    -- master has 2 objects, and they are linked
    assert(#master == 2)
    assert(obj2.links.pre == obj1)
    
    -- remove obj1 from master, and remove the variable reference
    table.remove(master, 1)
    obj1 = nil
    
    -- run gc manually
    collectgarbage("collect")
    
    -- master has only 1 object now, and the link has dissapeared
    assert(#master == 1)
    assert(obj2.links.pre == nil)
    
    print("Everything went as expected")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.