Singleton instance shared between plugin instances

Yes, static data is shared between all instances of a plugin if they are loaded in the same process and singletons are essentially static data. (They’re a static pointer to some heap memory which is shared between plugin instances).

This is a bit more complex because some hosts load plugins in separate processes so you can’t assume any static data will be shared between instances but that’s really the opposite of what you’re referring to here.


In general, singletons are a bad idea for many reasons but the simplest is that it makes it impossible to reason about ownership.

In brief, usually the best idea is to avoid shared state as much as possible. If you pass state to functions as you call them you decouple the classes so they don’t have to know about each other.

If you absolutely do need to pass some state down through your component hierarchy though the best bet is to have some kind of wrapper class that holds your “singleton” data and then you only have one thing for them all to reference.
However, this would mean that everything that references this data needs to know about it and all the functionality that comes along with that which again is generally bad practice. You’d tend to peel off bits of data so classes only reference what they absolutely have to.

One final word is to try and avoid sharing big blocks of data where possible and maximise a more MVC based approach.

But this is all a bit too complex to lay out in a few paragraphs. Just try and avoid singletons and reduce the amount of data that is shared as much as possible.

1 Like