Static storage for resources / singleton patterns and memory leaks


I have a class responsible for loading / managing all kinds of external resources. There's only one instance of it (implemented through a singleton), so that each application instance won't load anything twice - and also so that any code can access resources without carrying a reference to the main program instance.


However, one of the resources it manages are DrawableComposite's, which are Components, that, on destruction, acquires a message manager lock. Now since the storage duration of my manager is static, it's destructor is called after the execution of the main() function. This generates some subtle bugs, like the applicationbase class might free the singleton messagemanager at destruction. Now when the Components in my manager are destructed, they acquire a message manager lock and thus recreates the message manager, thereby actually leaking memory (nothing deletes it again).


Although it is a small amount (44 bytes i believe), it may still be a problem (who knows what happens if it isn't deleted correctly on applicated unloading?). Also how would you suggest to solve this problem? Avoiding singletons - that creates even more problems? I found the DeletedAtShutdown class, but it obviously requires your classes to be heapallocated, and does it guarantee how much of the JUCE runtime is still working at this point - like does this happen before or after the destruction of the message manager?


I feel like there are some pretty important gotcha's when using JUCE.

Edit: i just found that very page i was missing (, maybe just add the note that components shall be heap or stack allocated :)