LuauHeap Upvalue Memory Leak - How can I fix this?

I currently have a function in a module (down below). This function is flagged inside the “Unique References” section of the LuauHeap menu, meaning some sort of memory leak is happening here as it is not being garbage collected. The line given is line 1 below, and it stated it was an “upvalue” type. How can I fix this upvalue related memory leak?

I have other functions relating to this same memory leak in the same script, but this one is the most basic one so I’m sending this one. They all relate to some sort of event connection though. Since its an event connection, it seems less simple.

function Module:InitTrunk(Player, Car)
	local TrunkPart
	for _, Obj in Car.Body:GetChildren() do
		if Obj:IsA("BasePart") and Obj.Name == "Trunk" then 
			TrunkPart = Obj
			break
		end
	end
	if not TrunkPart then return end

	TrunkPart.Transparency = 1

	local Prompt = CreatePrompt.new({
		HoldDuration = 0,
		Name = "TrunkPrompt",
		MaxActivationDistance = 7,
		ActionText = "Open",
		ObjectText = TrunkPart:HasTag("Frunk") and "Frunk" or "Trunk",
		Parent = TrunkPart,
		Player = Player.Name
	})

	Prompt.Triggered:Connect(function(Plr)
		if Plr ~= Player then return end

		if Plr.Team == game.Teams.CIVILIAN then
			GUIEvent:FireClient(Player, "CivTrunk", TrunkPart:HasTag("Frunk") and "Frunk" or "Trunk")
		else
			GUIEvent:FireClient(Player, "Trunk", TrunkPart:HasTag("Frunk") and "Frunk" or "Trunk")
		end
	end)

	return Prompt
end

Anyone have any thoughts on how I can fix this?

1 Like

Hey Rapid!

Going from the code you’ve provided, you are sure the “Prompt” Instance is being destroyed
after you are done with it correct?

If yes, are you able to provide other code blocks that are writing to a variable (possibly a table) declared out of it’s scope?

if you don’t manually disconnect the connection when you’re done with it, it can hold onto references that stop the garbage collector from freeing up memory.

Prompt is destroyed when the car is destroyed, so yes it should. Maybe the car isn’t being GCed which is why this is being flagged?