Should I disconnect events when they are not in use?

I have a shop gui with image buttons that uses .Activated events to detect clicks. Currently, I’m connecting the events each time the player opens the shop and disconnecting them after the shop is closed. Should I continue to do this or should I connect all the events when the player game and leave it alone?

1 Like

You should most likely disconnect the events after they are no longer used or else it will leave a memory leak. It’s not garbage collected, sadly. Unless the connection was along with the script destroyed?

2 Likes

The events are reused every time the player opens the shop gui. I want to know if I should disconnect the events after they close the gui or keep the event active the entire play session.

1 Like

If your connections are going to be re-used it would be good to let them connected otherwise you’ll have to re-write the connections every time they open the gui.

1 Like

It makes no difference performance- wise, so unless you have a good reason to disconnect and reconnect them every time the inventory is closed and opened, you should go with the simpler approach which seems to be just setting up the connection and never disconnecting it.

Just setting up the connections once will not cause a memory leak. That only happens whey you repeatedly set up connections without disconnecting them, causing more and more connections to exist over time eventually filling the player’s PC’s memory. Neither method is going to cause or prevent more memory leaks, you only get them if you’re not doing things properly.

If you care about preventing memory leaks, make sure to also read up on

2 Likes