Is it a memory leak to pass a table to a function like this?

local MyTable = {}

local function modifyThisTable(theTable)
	theTable.A = "Beans"
end

modifyThisTable(MyTable)

I’m still learning about how tables can cause memory leaks by suspending scopes. Is it a memory leak to pass a table to a function to modify that table? (Does it prevent that table from ever being garbage collected due to being active in the function’s scope?)

The table won’t be garbage collected until theTable becomes nil. So if for some reason the function runs forever, such as like this:

local function modifyThisTable(theTable)
    while true do
        task.wait()
    end
end

The table won’t be garbage collected because that function will never end, so theTable will never ‘go out of scope’ and become nil.

2 Likes

okay, I see! The function ends and theTable is no longer a variable being used by anything
Thanks!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.