Hello! I have a module loader basically, and I wanted to know if this could/would cause a memory leak since the metatable references a variable in there.
Specifically this, but also anything else that you may spot:
loaded_modules[output.path] = setmetatable(output, {
__index = function(self, index)
return function()
warn(`Index "{index}" from module "{output.path}" doesn't exist. \n{debug.traceback("Traceback:", 2)}`)
end
end,
})
Heres the full code:
local model_folder = script.model
local view_folder = script.view
local loaded_modules = require(script.environment)
local function throw_loading_failure(index, module, message)
warn(`Failed to load module "{index}{module.Name}". {message}`)
end
local function load_modules_in_folder(folder: Folder)
local module_path_index = `{folder.Name}.`
for _, module in ipairs(folder:GetChildren()) do
local module_loading_complete = false
local loading_start_tick = tick()
local module_loading_thread = coroutine.create(function()
xpcall(function()
local output = require(module)
output:init(loaded_modules, module_path_index)
loaded_modules[output.path] = setmetatable(output, {
__index = function(self, index)
return function()
warn(`Index "{index}" from module "{output.path}" doesn't exist. \n{debug.traceback("Traceback:", 2)}`)
end
end,
})
module_loading_complete = true
end, function(module_loading_error_message)
throw_loading_failure(
module_path_index,
module,
`Error Output: \n{module_loading_error_message}`
)
end)
end)
coroutine.resume(module_loading_thread)
coroutine.wrap(function()
repeat
task.wait()
until module_loading_complete or tick() - loading_start_tick >= 2
if not module_loading_complete then
if coroutine.status(module_loading_thread) == "running" then
coroutine.close(module_loading_thread)
end
throw_loading_failure(
module_path_index,
module,
`Module yielded too long.`
)
end
end)()
end
end
shared.environment = loaded_modules
load_modules_in_folder(script.model)
load_modules_in_folder(script.view)
thank you!