# Singleton classes bug in DLL's?

**URL:** <https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477>\
**Category:** General JUCE discussion\
**Created:** [December 29, 2008, 3:33pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477 "2008-12-29T15:33:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zamrate](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zamrate](https://forum.juce.com/u/zamrate)\
**Post date:** [December 29, 2008, 3:33pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/1 "2008-12-29T15:33:54Z")

</div>

I think there might be a problem with the Singleton Class code of JUCE when using it in a DLL. I have problems/crashes that are caused by AudioPluginFormatManager::getInstance(). These problems arise in Cubase SX 3, but only if Cubase once scanned my plugin (I think it creates and deletes it again).

I think the problem arises from the fact that the AudioPluginFormatManager inherits from DeletedAtShutdown. This works nicely for .exe’s, but DLL’s will stay in memory and so their static values are not set back to 0, because the DeletedAtShutdown’s deleteAll() will only delete the objects, but not call deleteInstance() of the Singleton Class code which sets the \_singletonInstance variable back to 0.

So, next time the plugin gets loaded again, the \_singletonInstance variable of the singleton code will still contain a non-0 value and you get a crash.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 29, 2008, 5:23pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/2 "2008-12-29T17:23:59Z")

</div>

Agh… You’re catching some good bugs today! A singleton is supposed to call clearSingletonInstance() in its destructor, and it looks like I forgot to do so for that class. It should be:

`AudioPluginFormatManager::~AudioPluginFormatManager() throw()
{
    clearSingletonInstance();
}
`

I’ll get that sorted out right away.

---

<div class="post-metadata">

**Author:** ![zamrate](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zamrate](https://forum.juce.com/u/zamrate)\
**Post date:** [December 29, 2008, 5:44pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/3 "2008-12-29T17:44:59Z")

</div>

I think that will not be enough.  
In fact, the “alreadyInside” and “createdOnceAlready” variables will still be true, which will make bool problem become true in the getInstance() method.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 29, 2008, 5:52pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/4 "2008-12-29T17:52:22Z")

</div>

…no, I don’t think so. alreadyInside is only used briefly, and always gets reset. And createdOnceAlready is only used if allowOnlyOneInstance is true, and in this case it isn’t.

---

<div class="post-metadata">

**Author:** ![zamrate](https://avatars.discourse-cdn.com/v4/letter/z/f4b2a3/32.png) [@zamrate](https://forum.juce.com/u/zamrate)\
**Post date:** [December 29, 2008, 6:21pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/5 "2008-12-29T18:21:11Z")

</div>

Ok, true. But still allowOnlyOneInstance could be set back to false in the clearSingletonInstance() function to cover cases where the allowOnlyOneInstance parameter would indeed have been set to true!?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [December 29, 2008, 6:23pm UTC](https://forum.juce.com/t/singleton-classes-bug-in-dlls/3477/6 "2008-12-29T18:23:40Z")

</div>

The whole point about allowOnlyOneInstance is that it stops you re-creating the singleton during a run. Resetting it would defeat the whole point. But I don’t think that flag would cause any problems in a plugin anyway, unless you’re using it in your own classes.
