Disconnecting an Anonymous Function (listener)

Well, what I’d say is that memory leaks depends on how often the event takes place. Imagine having a touched event on a part.

It constantly detects whatever is touching it hence causes significant lag when overused and when not regulated (using if statements and the like)

Having MouseButton1(or2)Click events on multiple buttons is no big problem (But it suggests that you repeatedly coded every single button --lots of code), since the player isn’t constantly messing with the buttons unless, ofc, the game majorly involves GUI(s)

There’s only one case where I disconnect anonymous listeners and that’s if I’m using a Maid object or similar implementation. I’ll have a table on standby and table.insert anonymous functions into it. Later when I need to clean the table, I just iterate over it and call disconnect on all the connections.

In nearly every case where it looks like I’d need disconnect, I don’t use an anonymous function. You already lose your reference when you don’t assign it to a variable so trying to forge it back through potentially hacky means doesn’t make much sense. Don’t use anonymous functions or have a reference somewhere if you genuinely need to disconnect.

3 Likes

I’m so confused by this term ‘Maid’ , care to explain?
But I understand where you’re getting at with the table method of disconnecting events.

2 Likes