There seems to be a problem with tool connections (renderhumanoid’s descendant added connection) not being disconnected.
I added a few lines like checking if :Destroy()
was called and setting it into self.Destroyed
. Anyways, that’s not relevant,
I changed the descendantadded/removing connections to this:
Character.DescendantAdded:Connect(function(d)
print("Adding tool")
print("VF Handler has beed destroyed? " .. tostring(self.Destroyed))
if d:IsA("BasePart") then
humHandler.ObjHandlers[d] = self:RenderObject(d,FPS,CharacterClone)
end
end)
Character.DescendantRemoving:Connect(function(d)
print("Removing tool")
print("VF Handler has beed destroyed? " .. tostring(self.Destroyed))
if humHandler.ObjHandlers[d] then
humHandler.ObjHandlers[d]:Destroy()
end
end)
And obviously, you can see the gif, it’s still adding and removing even though it’s destroyed, which causes a lot of errors (and memory leaks).
I was able to fix this by doing the below:
- In
ViewportHandler.new
, I added a table to track event connections:
local Handler = {
HandlerID = HTTP:GenerateGUID(false);
Frame = Frame;
ObjectsRenderQueue = {}; --Active only
AllObjects = {}; --Includes inactive
EventConnections = {};
}
- In
ViewportHandler:Destroy
, I added:
for _, Connection in ipairs(self.EventConnections) do
Connection:Disconnect()
end
self.EventConnections = nil
- I changed
ViewportHandler:RenderHumanoid
's event connections:
local DescendantAddedConnection = Character.DescendantAdded:Connect(function(d)
if d:IsA("BasePart") then
humHandler.ObjHandlers[d] = self:RenderObject(d,FPS,CharacterClone)
end
end)
local DescendantRemovedConnection = Character.DescendantRemoving:Connect(function(d)
if humHandler.ObjHandlers[d] then
humHandler.ObjHandlers[d]:Destroy()
end
end)
table.insert(self.EventConnections, DescendantAddedConnection)
table.insert(self.EventConnections, DescendantRemovedConnection)
- I added the disconnects to
humHandler:Destroy
:
function humHandler:Destroy()
DescendantAddedConnection:Disconnect()
DescendantRemovedConnection:Disconnect()
for i,o in pairs(self.ObjHandlers) do
o:Destroy()
end
CharacterClone:Destroy()
end
Afterwards there were no errors in my console:
It says nil
because it only gets set when Destroy
is called, so basically it just means it wasn’t destroyed.
I encountered this because I’m putting the SurfaceGUI on a tablet tool to have a live feed on the go.