As you can see, I have refreshed my plugins a few times, and since plugins can’t delete related instances when they stop running, the instances they create never get destroyed. This issue doesn’t just affect instances in CoreGui, it affects anything any plugin creates that has been parented.
This is a potential memory leak, and just generally bad practice.
As a plugin developer, the preferred solution would be to let us access some signal (such as plugin.Removing) which we can then use to clean up instances. Another idea I had was giving instances a special signature when they are created by a plugin, so that when plugins are refreshed, instances with this signature will be destroyed – however there are flaws with this on its own, because plugins that create permanent instances (e.g. triangle terrain creators) will have their permanent instances removed on refresh.
Cool, filed! Could you provide some use cases as to why removing these instances after the plugin has stopped is essential, rather than destroying them before a plugin stops?
Well, firstly, to clarify: Destroying instances before the plugin stops is out of the question because these instances would likely still be in use at the time when the plugin is stopped.
Destroying instances once the plugin has stopped is essential because duplicates will be created otherwise. The most common version of this problem would be when a user causes their plugins to refresh while in a studio session (through updating their plugins probably). Every plugin the user is running restarts and none of the old instances are cleaned up. If this is the first refresh, this instantly doubles the number of instances in the session. Refreshes after that will continually duplicate. This is not good for performance (although the impact is negligible in most situations) and clutters the user’s environment.
Developers can work around this by checking to see if obsolete instances related to their plugin already exist before creating new ones, however it doesn’t fix the actual problem (instances not being cleaned up) and only works if the plugins are started again (so if they stop and don’t restart, the issue is still there).
That’s really the only use case I have at the moment.
It is important and essential to remove them after the plugins stop because there is a chance that plugins will not stop gracefully. This happens when studio crashes or when plugins are reloaded, and while it’s not incredibly common, it can build up over time, and cause minor performance issues and clutter, as EmeraldSlash already said.
While it’s technically possible to just set the Archivable property of an Instance to false to prevent these duplications from being saved, this still doesn’t deal with Instances that are duplicated while Studio is open, and it requires plugin developers to actively manage this, which most don’t.