Singleton && DeletedAtShutdown Class in Plug-In


#1

Hey Jules

I have a class that is derived from DeletedAtShutdown and uses the juce_DeclareSingleton macro that’s not exactly behaving the right way. I have “allowOnlyOneInstance” set to true… When the last plug-in is deleted and my class is deleted it never comes back when a new plug-in gets created. Is this correct? I’m using the static getInstance() method to create it in my filter constructor.

TestFilter::TestFilter() { MyClass::getInstance(); }

I’ve set-up a couple of break-points in my singleton/DeletedAtShutdown class, one in the constructor and one in the destructor, just to make sure I’m not going bonkers here, and this is what I’m getting:

  1. Instantiate (First Plug-In)
    The singleton/DeletedAtShutdown class constructor gets called. (Correct)

  2. Instantiate (Second Plug-In)
    The singleton/DeletedAtShutdown class constructor does NOT get called. (Correct, only one instance allowed)

  3. Delete (First Plug-In)
    The singleton/DeletedAtShutdown class destructor does NOT get called. (Correct, one more plugin exists)

  4. Delete (Second Plug-In)
    The singleton/DeletedAtShutdown class destructor gets called. (Correct, deleted at shutdown)

  5. Instantiate (New Plugin)
    The singleton/DeletedAtShutdown class constructor does NOT get called. (?)

Does that look right?

Thank You


#2

Hmm, I think you’re misunderstanding what “allowOnlyOneInstance” means. It doesn’t mean “only allow one at a time” (which would be pointless, because that’s the whole point of a singleton). It means “only create one of these during the entire run of the program” - i.e. once it’s been deleted, don’t create another one again. It’s handy for things like checking that an app doesn’t accidentally re-create an expensive object during its shutdown code, but isn’t at all relevant for plugins.

Maybe it needs better explanation in the docs?


#3

Ah yes, that did throw me off.


#4

Looking at the docs again, I’m actually going to rename that parameter to make it less confusing. “doNotRecreateAfterDeletion” is a better name.


#5

indeed.

I was basically looking for a really easy, effortless way to wrap some ugly static c functions that only need to get called once on launch and once when all the plugs have gone to clean up (easy to read the code). I’ve thrown away the Singleton idea but still using DeletedAtShutdown and a global pointer that gets zeroed out and created etc.

Thank You


#6