Is this a memory leak?

clone.MouseButton1Down:Connect(function()) do --- makes a connection

clone:Destroy() ---- destroys the whole clone a while later, no disconnection beforehand

3 Likes

Iā€™m pretty sure that when an object is destroyed all connections attached to it disconnect automatically, to test this, after you destroy it you can print

print(type(clone)) (I meant to do this for the connection, find a way to initialize it outside of the function to debug that)

Btw not sure that will even work but worth a try, if itā€™s type is rbxscriptconnection then Iā€™m pretty sure it hasnā€™t disconnected

4 Likes

to test if its disconected u need to make local function

local test = clone.MouseButton1Down:Connect(function() end) 

print(type(test)) -- should print function
clone:Destroy() 
print(type(test)) -- if nil or error then its disconected
4 Likes

the code prints ā€˜userdataā€™ on both

1 Like

then try

local test = clone.MouseButton1Down:Connect(function() end) 

print(test)
clone:Destroy() 
print(test)
1 Like

the code prints ā€œconnectionā€ on both

the connections are not cleared when destroying a part, but it does get disconnected, there is a read-only value called RBXScriptConnection.Connected

the right way to see if its connected would be this:

local test = clone.MouseButton1Down:Connect(function() end) --hold a reference to the connection

print(test.Connected) --true
clone:Destroy() --all connections connected to signal will be disconnected
print(test.Connected) --false

and yes, connections are disconnected when destroying instance.

3 Likes

That is what I tried to find. It will be helpful to know

1 Like

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