I’d love to see a solution if not a perfect one. After finding out about this I realized it actually has a big impact on… Maybe 70% of the server sided code I’ve ever written
I always assume when I call :Destroy()
any connections on that object are deleted regardless of it being on the server or client.
I knew that instances were not destroyed on the client when it was originally done on the server but I never realized this would cause a memory leak, and, its really counter intuitive as to how that could cause memory leaks.
I think the best way to fix this issue is likely to simply not lock the instance when its destroyed over the network. For better compatibility, descendants can have their events disconnected but not their parents changed. The effect is server-destroyed instances have all events disconnected on the client but nothing else changes.
I know there’s more to the :Destroy() call than just disconnecting events (I think there’s like some weird magic going on because destroyed instances that didn’t have event connections take up literally like 100x less memory)
That would mean the server has to send when a specific instance gets destroyed (which I mean, makes it easier for a Destroyed event to exist too )
This would fix the memory leak issue around this without likely causing many issues. The only difference would be that events would stop working.
I would not think that a pattern like that is used at all because it would be harder to use than not, your game would have to rely on the existence of an event for an object (which is in nil, so, not a button or user input thing, not a part, etc) :Destroy()
ed from the server or an object :Destroy()
ed from the server that has been reparented to the workspace. (The first being a really specific situation that I would think is already questionable for someone to end up using and the second being less unlikely but still pretty unlikely)
(If breakage is a concern adding a temporary fast flag is always an option so it can be quickly reverted)
Lastly, an example of this effecting me is my game’s entity code. It probably suffers from this because the server notifies the client of an entity creation (but not deletions) and tells the client what module to load and the instance for the entity (as well as any data for the entity state). A bunch of the different entities in my game connect to remote events and connect to various property changes and things under the entity’s instance. When the entity is destroyed on the server, the connections would stay active on the client because I assume they’ll be disconnected if it gets deleted.
I can easily fix this by telling the client when an entity is destroyed and having the client perform the same call, but, it’s still less than ideal, and, its not at all clear to probably anyone that they should be doing this. (It’s not mentioned on the devhub for the Destroy documentation and I never realized this problem)